Skip to content

Instantly share code, notes, and snippets.

@nycdotnet
Last active September 13, 2017 13:20
Show Gist options
  • Save nycdotnet/9d385e847274266e0525 to your computer and use it in GitHub Desktop.
Save nycdotnet/9d385e847274266e0525 to your computer and use it in GitHub Desktop.
Java update PowerShell script
# This script will uninstall anything like *Java* from Add/Remove Programs, Unblock and Install a new JDK,
# and set the JAVA_HOME environment variable for the new version.
#
# You have to set the $jdkInstallerPath first to the location where you got the JDK. Such as:
# $jdkInstallerPath = "C:\Users\MyAccount\Downloads\jdk-7u60-windows-x64.exe"
#
# WARNING: This will uninstall ANYTHING that says Java anywhere in the name!!! Use with caution!!!
#
# Note: This assumes that you are using the x64 JDK on 64-bit Windows. Not tested in other scenarios.
if ($jdkInstallerPath -eq $null) { "You must define `$jdkInstallerPath such as `$jdkInstallerPath=`"C:\Users\MyAccount\Downloads\jdk-7u60-windows-x64.exe`"" } else { "Uninstalling old Java..."; Get-WmiObject -Class win32_product | ? {$_.Name -like "*Java*"} | % { "Uninstalling " + $_.Name ; msiexec /x "$($_.IdentifyingNumber)" /qn | Out-Null}; "Unblocking $jdkInstallerPath ..."; cmd /c "echo.>`"$jdkInstallerPath`":Zone.Identifier" ; $silentSwitch = "/s"; "Installing new Java..."; start-process -FilePath $jdkInstallerPath -ArgumentList $silentSwitch -Wait |Out-Null; $JavaFolderName = dir "C:\Program Files\Java\jdk*"; if ($JavaFolderName -is [System.IO.DirectoryInfo]) {$JavaFolderName = $JavaFolderName.FullName + "\bin"} else {$JavaFolderName = $JavaFolderName[0].FullName + "\bin"}; [Environment]::SetEnvironmentVariable("JAVA_HOME",$JavaFolderName,"Machine"); "Set JAVA_HOME to $JavaFolderName ."; }
@Durgairaja
Copy link

what is "Unblocking $jdkInstallerPath ...";
cmd /c "echo.>"$jdkInstallerPath":Zone.Identifier" ; means i am new to ps why :Zone.Identifier is here required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment