java powershell scripting

PowerShell: Download and Install Java 8 JRE

Update: This is an older script that I had written for work, and it hasn’t been tested for a long time. The idea of the script was to use PowerShell to silently download and install Java 8 JRE. Since Oracle has change their licensing terms on using Java in business environments, I recommend using Adoptium Eclipse Temurin as an open-source alternative.

Back to the Script

This script I wrote for our automation system that will:

  1. Download the latest Java 8 JRE from Java.com
  2. Save in a local directory C:\Install
  3. Create a Install Options File
  4. Run the installer silently with the necessary Installer Options

The Installer Options file determines what configuration is used the install of Java.

For example, in the script I have configured. This will perform a silent install, auto updates enabled, no reboot after the installation, Sponsors/ads install disabled and will uninstall any older existing installations of Java JRE.

  • INSTALL_SILENT=Enable
  • AUTO_UPDATE=Enable
  • REBOOT=Disable
  • SPONSORS=Disable
  • REMOVEOUTOFDATEJRES=Enable

You can find these and other options to use from the Java website:

https://docs.oracle.com/javase/8/docs/technotes/guides/install/config.html#installing_with_config_file

This has been tested to work on PowerShell v2 and higher.

PowerShell Script

--------------------------------------------------------------------------
Deploy Java JRE Software
Date: 21-Sept-2016
Created by: Daniel Burrowes
--------------------------------------------------------------------------

Description:
Downloads the Java installer and installs silently

--------------------------------------------------------------------------

# -Verbose and -Debug

[CmdletBinding()]
param( )

# --------------------------------------------------------------------------

Write-Verbose "Setting Global Variables..."
$InstallDir = "c:InstallJava"
$Source = "http://javadl.oracle.com/webapps/download/AutoDL?BundleId=211996"
$Destination = "$InstallDirjava.exe"
$Options = "$InstallDirjava_options.txt"


#Create install directory
Write-Verbose "Creating Install Directory"
New-Item -Path $InstallDir -ItemType directory -Force

Write-Verbose "Downloading Software..."
$start_time = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Source, $Destination)
Write-Verbose "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

Write-Verbose "Creating Installer Options File..."
$text = 'INSTALL_SILENT=Enable
AUTO_UPDATE=Enable
REBOOT=Disable
SPONSORS=Disable
REMOVEOUTOFDATEJRES=Enable
'

# Create file
$text | Set-Content $Options

#Running the installer
Write-Verbose "Executing Java Install.."
Start-Process -FilePath $Destination -ArgumentList "INSTALLCFG=$Options /s /L $InstallDirjre-install.log" -Wait -Verbose -PassThru

Check out my other PowerShell related articles here!

Leave a Reply

Discover more from Daniel Bs Tech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading