I keep getting an “Illegal characters in path” error when the script gets to the Execute-Process line in this code snippet. I don’t understand why. The actual value that $uninstallString returns when I run it under Powershell ISE is “C:\Program Files (x86)\Australian Monitor\ZONEMIX\ZONEMIX Control Software 1.3.0\uninstall.exe” and when I put that value in place of $uninstallString, the script works.
foreach ($regPath in $regPaths) {
# Get all subkeys (applications) from the registry path
$keys = Get-ChildItem -Path $regPath
foreach ($key in $keys) {
# Get the display name and uninstall string
$displayName = (Get-ItemProperty -Path $key.PSPath -Name "DisplayName" -ErrorAction SilentlyContinue).DisplayName
$uninstallString = (Get-ItemProperty -Path $key.PSPath -Name "UninstallString" -ErrorAction SilentlyContinue).UninstallString
# Check for partial match
if (($displayName -like $partialAppName) -and ($uninstallString -ne $null)) {
if ($uninstallString.StartsWith("msiexec.exe /i",'CurrentCultureIgnoreCase') ) {
$msiProductCode = $uninstallString -replace "msiexec.exe /i"
Execute-MSI -Action 'Uninstall' -Path "$msiProductCode"
} else {
Execute-Process -Path "$uninstallString"
}
}
}
}
maybe not the answer, but I usually do a write-host “Uninstallstring: $Uninstallstring”
then you can possibly see what’s being presented to the variable to troubleshoot this kinda stuff.
You often don’t get what you expect. For example, the uninstall value often contains -silent or something else. This means you have to split the string. I use this code to uninstall:
Maybe there’s a hidden whitespace or ctrl character in the registry value. Visual Studio Code is good at rendering these to help you understand if there’s something in there unexpected that you can’t see. I’d try copying the value of the registry key from regedit and pasting it into vscode to see if it can show you.