0

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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
user770022
  • 2,899
  • 19
  • 52
  • 79
  • SAP is a company. Good luck trying to uninstall that... – vwegert Jun 29 '16 at 14:06
  • Sorry SAP GUI is the application. – user770022 Jun 29 '16 at 14:09
  • Check the Installation Guide - the installer has a command line parameter for this (/uninstall /all, I believe). – vwegert Jun 29 '16 at 14:21
  • Thanks but I'm trying to learn PS so its something I would like to complete with PS. I found the command thats in the install guide nwsapsetup.exe /uninstall /all /nodlg /force and that does work but I would like to do it with PS – user770022 Jun 29 '16 at 14:36

2 Answers2

1

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.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • Same error but 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 "$app" -verb runas -Wait – user770022 Jun 29 '16 at 13:59
  • Soda... The issue is from this `select uninstallstring`..... Update your answer with that – Matt Jun 29 '16 at 14:04
  • What I answered is what struck me first. I had to run the code at least once to spot the other problems. Answer updated. – sodawillow Jun 29 '16 at 14:09
  • Thank you for being helpful. – user770022 Jun 29 '16 at 14:12
  • I prefer to use WMI to do this but that's another subject. Some good advice is included in Bacon Bits' answer. Edit : check my answer for this question for the WMI method : http://stackoverflow.com/questions/34679597/uninstalling-using-get-wmiobject/34680914#34680914 – sodawillow Jun 29 '16 at 14:36
  • $app = Get-WmiObject -Class Win32_Product -Filter "Vendor = 'SAP AG'" only gives me – user770022 Jun 29 '16 at 14:40
  • IdentifyingNumber : {A47A9101-6EB5-4314-BDA1-297880FBB908} Name : Microsoft redistributable runtime DLLs VS2008 SP1(x86) Vendor : SAP AG Version : 9.0 Caption : Microsoft redistributable runtime DLLs VS2008 SP1(x86) – user770022 Jun 29 '16 at 14:40
  • You should try to understand the code before running it :). If you see this I think it's running properly and uninstalling the application. Sorry if this methods fails you. Please create a new question if you have an issue with this other piece of code. – sodawillow Jun 29 '16 at 14:44
  • I think the problem with the WMI method is that Win32_Product is not a comprehensive listing of all software that has been installed on the system. It's not even a list of software that's been installed with Windows Installer. Windows' Add/Remove Programs list never matches whats in Win32_Product. – Bacon Bits Jun 29 '16 at 16:08
  • The list is not as complete as the uninstall registry keys, but I still find this method useful in many cases. – sodawillow Jun 29 '16 at 16:36
1

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.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • What does this piece mean? -ArgumentList '/C' – user770022 Jun 29 '16 at 15:44
  • @user770022 The `-ArgumentList` parameter specifies the array of arguments, in order, to pass to `cmd.exe` when it starts. The `/C` tells `cmd.exe` to carry out the command and then terminate. See `cmd.exe /?` for more information about that. What my workaround is doing is starting the command line and passing the uninstall string to that to execute, so PowerShell is starting CMD, and CMD is starting the uninstallation. – Bacon Bits Jun 29 '16 at 16:03