Uninstalling an application via uninstallString using powershell in WPF application

Viewed 1698

I am trying to uninstall software using below Powershell script in an WPF application

get-package |where name -like "Notepad++ (64-bit x64)" |% { & $_.Meta.Attributes["UninstallString"] /S}

the above command works only for Notepad++ (64-bit x64) but fails when I tried with software's like Git version 2.25.1 and TortoiseGit 2.10.0.0 (64 bit)

For Git version 2.25.1

get-package |where name -like "Git version 2.25.1" |% { & $_.Meta.Attributes["UninstallString"] /S} 

I am getting the below error:

& : The term '"C:\Program Files\Git\unins001.exe"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:59 + ... "Git version 2.25.1" |% { & $_.Meta.Attributes["UninstallString"] /S} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: ("C:\Program Files\Git\unins001.exe":String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

For TortoiseGit 2.10.0.0 (64 bit)

 get-package |where name -like "TortoiseGit 2.10.0.0 (64 bit)" |% { & $_.Meta.Attributes["UninstallString"] /S} 

I am getting below error:

The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object. At line:1 char:70 + ... t 2.10.0.0 (64 bit)" |% { & $_.Meta.Attributes["UninstallString"] /S} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : BadExpression

I also tried using WMI objects powershell script below is the script

Get-WmiObject -Class Win32_Product | Where {$_.Name -like \"{returnStr}\"}  | foreach {$_.Uninstall()}".Replace("{returnStr}", Notepad++ (64-bit x64))   

Above script only works for the software's that were installed via an MSI

If anyone has suggestions on how to proceed it would be much appreciated!

2 Answers

You can find Uninstall-Package your friend here:

get-package | where-object name -like "Notepad++ (64-bit x64)" | ForEach {uninstall-package}

You have to get rid of the double quotes. Replacing " with $null or no 2nd arg. Uninstall-package doesn't work for non msi installs. Also this only works in windows powershell 5.1, not powershell 7 currently. Does TortoiseGit have an uninstallstring?

get-package "*Git version 2.25.1*" | 
  % { & ($_.Meta.Attributes["UninstallString"] -replace '"') /S} 
Related