Get-PC/Get-PC/Private/UninstallProgram.ps1

39 lines
2.1 KiB
PowerShell

function Invoke-UninstallProgram ($Computer) {
Invoke-Command -ComputerName $Computer -SessionOption $(New-PSSessionOption -MaxConnectionRetryCount 1 -NoMachineProfile) -ScriptBlock {
$target = Read-Host "Name of app to uninstall"
$apps = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -match $target}
if (-not ($apps -is [array])) {
$apps = @($apps)
}
if ($apps.Length -eq 0) {
Write-Host -ForegroundColor Red "No app matching '$target' found on $Computer"
return
}
for ($i=0; $i -lt $apps.Length; $i++) {
Write-Host "[$i] $($apps[$i].Name)"
}
$selection = Read-Host "Which app(s) do you want to uninstall?"
if (-not ($selection)) { return }
$selection = -split $selection
foreach ($item in $selection) {
Write-Host "[$item] $($apps[$item].Name)"
}
$res = Read-Host "Are you sure you want to delete this/these app(s)? (y/N)"
if (-not ($res -match "^[yY]")) {
Write-Host -ForegroundColor Red "Uninstall Aborted"
return
}
foreach ($item in $selection) {
Write-Host "Uninstalling $($apps[$item].Name)"
$res = $apps[$item].Uninstall()
switch ($res.ReturnValue) {
0 { Write-Host "Successfully Uninstalled $($apps[$item].Name) from $Computer" }
1641 { Write-Host "Successfully Uninstalled $($apps[$item].Name) from $Computer"; Write-Warning "Restarting $Computer" }
3010 { Write-Host "Successfully Uninstalled $($apps[$item].Name) from $Computer"; Write-Warning "Please restart $Computer to finish uninstall" }
3011 { Write-Host "Successfully Uninstalled $($apps[$item].Name) from $Computer"; Write-Warning "Please restart $Computer to finish uninstall" }
default { Write-Warning "$appname failed to uninstall from $Computer with ReturnValue $($res.ReturnValue). See an MsiExec return value table for more information." }
}
}
}
}