Get-PC/Private/UninstallProgram.ps1

35 lines
1.4 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()
if ($res.ReturnValue) {
Write-Warning "$appname failed to uninstall from $Computer with ReturnValue $($res.ReturnValue)"
}
}
}
}