0

The various paths from the variable "$EXECleanUninstallStringsX86" are always transferred all at once and not individually. Although two processes are started for two paths, the paths of both processes always include all paths from the variable:

This command cannot be run due to the error: An error occurred trying to start process 'C:\Program Files (x86)\7-Zip\Uninstall.exe C:\Program Files (x86)\Test\Test.exe' with working directory 'C:\PatchTemp'.

Here is the relevant part from my script:

$DisplayNameX86 = "7-Zip*"
$EXEUninstallArgumentsX86 = "/S"

# Uninstall (EXE/X86)
If ($DisplayNameX86 -ne "") {
$EXEUninstallStringsX86 = Get-ItemProperty -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -like $DisplayNameX86 -and $_.UninstallString -like "*.exe*" -and $_.UninstallString -notlike "*MsiExec*"} | Select-Object Publisher,DisplayName,DisplayVersion,UninstallString
If ($EXEUninstallStringsX86 -ne $null) {
ForEach ($EXEUninstallStringX86 in $EXEUninstallStringsX86){
$EXECleanUninstallStringsX86 = ($EXEUninstallStringsX86.UninstallString -Replace '.exe.*','.exe' -Replace '"','')
Start-Process -FilePath "$EXECleanUninstallStringsX86" -ArgumentList "$EXEUninstallArgumentsX86" -Wait -PassThru
Write-Host "EXE-Uninstallation(s) for x86 started" -f Green
}
} Else {
Write-Host "EXE-UninstallString(s) for x86 not found" -f Yellow
}
}

This is what the content of a variable "$EXECleanUninstallStringsX86" looks like, for example:

C:\Program Files (x86)\7-Zip\Uninstall.exe
C:\Program Files (x86)\Test\Test.exe

I have tested countless variants (e.g. cmd /c) without success.

2
  • You're using the array variable $EXEUninstallStringsX86 instead of the loop variable $EXEUninstallStringX86 inside your loop. I'd recommend to us more speaking variable names. If it's an array or a list I'd have the actual word "list" in the variable name. ☝🏼😉 ... somthing like UninstallString_List Commented Apr 21, 2024 at 23:16
  • Isn't that a bad joke? I have been looking for the error all day and the cause is the wrong name of the variable. I am totally desperate because I was sure that the basic structure was correct. This clearly shows that you are right. I will give the variables more meaningful names in future. Thank you! Commented Apr 21, 2024 at 23:37

1 Answer 1

0

The cause was that I used the wrong variable within the foreach-loop. Here is the correct script:

$DisplayNameX86 = "7-Zip*"
$EXEUninstallArgumentsX86 = "/S"

# Uninstall (EXE/X86)
If ($DisplayNameX86 -ne "") {
$EXEUninstallStringsX86 = Get-ItemProperty -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -like $DisplayNameX86 -and $_.UninstallString -like "*.exe*" -and $_.UninstallString -notlike "*MsiExec*"} | Select-Object Publisher,DisplayName,DisplayVersion,UninstallString
If ($EXEUninstallStringsX86 -ne $null) {
ForEach ($EXEUninstallStringX86 in $EXEUninstallStringsX86){
$EXECleanUninstallStringsX86 = ($EXEUninstallStringX86.UninstallString -Replace '.exe.*','.exe' -Replace '"','')
Start-Process -FilePath "$EXECleanUninstallStringsX86" -ArgumentList "$EXEUninstallArgumentsX86" -Wait -PassThru
Write-Host "EXE-Uninstallation(s) for x86 started" -f Green
}
} Else {
Write-Host "EXE-UninstallString(s) for x86 not found" -f Yellow
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.