PowerShell: Uninstall Java from Windows computers

Here’s a PowerShell script to uninstall Java from Windows computers. This script uses the Get-WmiObject cmdlet to find the Java application and the Invoke-Expression cmdlet to uninstall it.

cmdlet to uninstall it.

powershellCopy code# PowerShell script to uninstall Java from Windows computers

# Function to uninstall a program by name
function Uninstall-Program {
    param (
        [string]$programName
    )

    # Get the list of installed programs
    $installedPrograms = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like "*$programName*" }

    foreach ($program in $installedPrograms) {
        Write-Host "Uninstalling $($program.Name)..."
        $program.Uninstall() | Out-Null
        Write-Host "$($program.Name) has been uninstalled."
    }
}

# Uninstall Java
$javaProgramName = "Java"
Uninstall-Program -programName $javaProgramName

Write-Host "Java uninstallation process completed."

This script performs the following steps:

  1. Defines a function Uninstall-Program that takes the program name as a parameter.
  2. Uses Get-WmiObject to retrieve a list of installed programs that match the provided name.
  3. Iterates through the list of matching programs and uninstalls each one.
  4. Uninstalls Java by calling the Uninstall-Program function with “Java” as the program name.

You can run this script in a PowerShell window with administrative privileges.