Well you do a "foreach ($App in $AppName)" and the first thing that you do in the script block is to overwrite the $app variable. You don't have any error handling to test to see that you actually found an app to uninstall. On the start-process you include the msiexec switches that you already added to the $uninstall variable.
Try this and see if it works. Uncomment the start-process when you have tested it and actually want to run the uninstall.
# Updated 20-December, test for admin level and multiple apps, add logging to msiexec.
$Appname = @(
'Agent Ransack'
)
$admin = (whoami.exe /all | select-string S-1-16-12288) -ne $null
if ($admin -ne $true){
'You are not running Powershell in administrator mode. (UAC is not elevated)'
"This script probably won't work."
return
} else {
'You are running Powershell in administrator mode.'
}
foreach ($App in $AppName) {
""
"Looking for $app"
$RegApp = Get-Itemproperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName |
Where-Object { $_.DisplayName -match "^*$app*"}
if ($Regapp.Count -gt 1) {
"Multiple applications were found."
"Please make the search string unique."
($regapp).displayname
continue
}
if($RegApp) {
"We found $app"
if($RegApp.Uninstallstring -match '^msiexec') {
$UninstallString = "$($RegApp.UninstallString -replace '/I', '/X' ) /qn /norestart /log C:\Windows\temp\msi.log"
} else {
"But it does not use MSIEXEC!"
$UninstallString = $RegApp.UninstallString
}
#### $RegApp.UninstallString
$pos = $UninstallString.ToLower().IndexOf('.exe')
$exe = $UninstallString.Substring(0,$pos + 4)
$args = $UninstallString.Substring($pos + 4).trim()
"The uninstall string is: $UninstallString"
"The program is ........: $exe"
"The arguments are......: $args"
##### Uncomment the next line to actually run the uninstall.
##### Start-Process -FilePath $exe -ArgumentList $args -NoNewWindow -Wait
Start-Process -FilePath $exe -ArgumentList $Args -Wait
"It's finished. Here is the log."
get-content C:\Windows\temp\msi.log
} else {
"We did not find $App"
}
}