user770022
user770022

Reputation: 2959

Remove all SAP GUI versions

I'm working on a script to remove all versions of SAP GUI here is what I have

$uninstall=Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object displayname -like "*SAP*" | select uninstallstring
foreach ($app in $uninstall)
{
    Start-Process "$uninstall" -verb runas -Wait

}


Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:5 char:9
+         Start-Process "$uninstall" -verb runas -Wait
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:5 char:9
+         Start-Process "$uninstall" -verb runas -Wait
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process],   InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Upvotes: 0

Views: 3712

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32200

I think you need to do it like this:

foreach ($app in $uninstall) {
    Start-Process -FilePath 'C:\Windows\System32\cmd.exe' -ArgumentList '/C',$app -Verb RunAs;
}

The problem is that UninstallString typically contains the path to an executable with arguments. Neither Start-Process, nor Invoke-Expression, nor the call operator (&) like that. They want the path to the executable, and then the arguments to be in another list.

Compare:

Start-Process 'msiexec.exe /?';

With:

Start-Process 'C:\Windows\System32\cmd.exe' -ArgumentList '/C','msiexec.exe /?';

The other option is to try to parse UninstallString and split the arguments up, but that's pretty gross.

Upvotes: 1

sodawillow
sodawillow

Reputation: 13176

I guess you want to try

Start-Process "$app" -Verb RunAs -Wait

$uninstall represents the complete collection, $app is a single item.

Also, $uninstall does not hold what you expect, you should try like this:

regPath = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"

$uninstall = Get-ItemProperty $regPath |
    Where-Object DisplayName -like "*SAP*" |
    Select-Object -ExpandProperty UninstallString -ErrorAction SilentlyContinue

foreach ($app in $uninstall) {
    #do something with $app
}

-ExpandProperty makes sure you only get the UninstallString values listed in $uninstall.

-ErrorAction SilentlyContinue removes the errors caused by missing UninstallString values.

Last but not least, I have tried to run the command with Start-Process and it fails, you will have to use another method to execute the uninstall command.

Upvotes: 1

Related Questions