So as the title says I am trying to put together a powershell script that allows me to enter in the name of a computer, then enter in the name of some software and then it will uninstall that software. Specifically right now I am trying to get it to uninstall Microsoft Visio Standard 2013 and Microsoft Project Standard 2013. The script does not appear to work with any software I specify, despite it stating the uninstall completes. However with the two previously mentioned programs it fails with an error code 1639 and I am not sure why. The script should search for any specifically named uninstall methods, in this case I have three, one for chrome, one for adobe, and one for office in general and if none of those match it moves on to try two 'generic' uninstall methods. I guess basically I need to know what special parameters Microsoft Visio Standard 2013 and Microsoft Project Standard 2013. If possible I'd like to know if there is something similar with Microsoft Project Standard 2016. With Chrome it just straight up says it can find the file. The goal is to be able to specify a software by name and have it be uninstalled remotely and silently. Obviously having it work for every program ever is impossible but if there is a reference resource that I can look at specifically for all microsoft applications that would be great.
Error Log when trying to uninstall project standard:
Microsoft Project Standard 2013: Uninstallation failed with exit code: 1639
Microsoft Project Standard 2013: Uninstallation failed with exit code: 1639
Here is a copy of the script:
# Prompt for administrator credentials
$credentials = Get-Credential -Message "Enter Administrator Credentials"
# Enter domain computer name
$computerName = Read-Host "Enter the domain computer name"
# Enter the name of the software to be uninstalled
$softwareName = Read-Host "Enter the name of the software to uninstall"
# Specify the log file path
$logFilePath = "C:\Users\***\Documents\uninstall.log"
# Function to uninstall software by product code
function Uninstall-SoftwareByProductCode {
param (
[Parameter(Mandatory = $true)]
[string]$ProductCode
)
$arguments = "/x $ProductCode /qn /norestart"
Write-Host "Uninstalling software with product code: $ProductCode"
try {
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Software successfully uninstalled."
} else {
throw "Uninstallation failed with exit code: $($process.ExitCode)"
}
} catch {
Write-Host "Error: $_"
$errorMessage = "${softwareName}: $_"
$errorMessages += $errorMessage
}
}
# Function to find product code by software name
function Get-ProductCodeByName {
param (
[Parameter(Mandatory = $true)]
[string]$SoftwareName
)
$key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
$subkeys = Get-ItemProperty -Path $key | Where-Object { $_.DisplayName -like "*$SoftwareName*" }
if ($subkeys) {
$subkey = $subkeys | Select-Object -First 1
return $subkey.PSChildName
} else {
return $null
}
}
# Initialize error messages variable
$errorMessages = @()
# Method 1: Adobe Acrobat
if ($softwareName -eq "Adobe Acrobat") {
$productCode = Get-ProductCodeByName -SoftwareName $softwareName
if ($productCode) {
Uninstall-SoftwareByProductCode -ProductCode $productCode
exit
}
}
# Method 2: Microsoft Office
if ($softwareName -eq "Microsoft Office") {
$productCode = Get-ProductCodeByName -SoftwareName $softwareName
if ($productCode) {
Uninstall-SoftwareByProductCode -ProductCode $productCode
exit
}
}
# Method 3: Google Chrome
if ($softwareName -eq "Google Chrome") {
Write-Host "Uninstalling Google Chrome"
try {
$process = Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\uninstall.exe" -ArgumentList "/S" -Credential $credentials -NoNewWindow -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Google Chrome successfully uninstalled."
} else {
throw "Uninstallation failed with exit code: $($process.ExitCode)"
}
} catch {
Write-Host "Error: $_"
$errorMessage = "${softwareName}: $_"
$errorMessages += $errorMessage
}
exit
}
# Method 4: Custom Uninstall Methods
# Uncomment and add your custom uninstall methods here
# Method 5: Common MSI Silent Uninstall
Write-Host "Attempting common MSI silent uninstall method"
$arguments = "/x {product code goes here} /qn /norestart"
try {
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Software successfully uninstalled using common MSI method."
exit
} else {
throw "Uninstallation failed with exit code: $($process.ExitCode)"
}
} catch {
Write-Host "Error: $_"
$errorMessage = "${softwareName}: $_"
$errorMessages += $errorMessage
}
# Method 6: Common Product Code Silent Uninstall
Write-Host "Attempting common product code silent uninstall method"
$arguments = "/x {product code goes here} /qn /norestart"
try {
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Software successfully uninstalled using common product code method."
exit
} else {
throw "Uninstallation failed with exit code: $($process.ExitCode)"
}
} catch {
Write-Host "Error: $_"
$errorMessage = "${softwareName}: $_"
$errorMessages += $errorMessage
}
# Generate error log if any errors occurred
if ($errorMessages) {
$errorLogPath = "C:\Users\user.name\Documents\uninstall.log"
Write-Host "Generating error log at $errorLogPath"
$errorMessages | Out-File -FilePath $errorLogPath -Encoding UTF8
} else {
Write-Host "All uninstallation methods failed. No errors to log."
}