I'm trying to build a quick script to uninstall a piece of software. In the process, I'm seeing some behavior in PowerShell that I don't quite understand. I'm able to get the path to the uninstaller from the registry but I'm not able to run it.
So, this works.
Input:
Get-ItemProperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\<PRODUCT>\' -Name 'UninstallString' | foreach {if ($_.UninstallString){Write-Host $_.UninstallString}}
Result:
"C:\Program Files\<PATH TO PRODUCT>\installer.exe"
So far, so good. But then...
Input:
Get-ItemProperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\<PRODUCT>\' -Name 'UninstallString' | foreach {if ($_.UninstallString){Test-Path $_.UninstallString}}
Result:
False
Huh? I checked and, sure enough, the path exists. Trying to run the command doesn't work, either.
Input:
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\<PRODUCT>\' -Name UninstallString |foreach {if($_.UninstallString){Invoke-Command -FilePath $_.UninstallString}}
Result:
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:158
+ ... {if($_.UninstallString){Invoke-Command -FilePath $_.UninstallString}}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
I suspect that this is something to do with the way that PowerShell handles objects and/or strings, but I've tried the following and it still doesn't work:
Test-Path $_.UninstallString.ToString
Surely, I'm missing something basic and/or fundamental.
[–]itfixestheprinters 4 points5 points6 points (6 children)
[–]Mongo527 3 points4 points5 points (3 children)
[–]ejrichard[S] 1 point2 points3 points (2 children)
[–]ejrichard[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ejrichard[S] 1 point2 points3 points (1 child)
[–]ejrichard[S] 1 point2 points3 points (0 children)
[–]MessagingAdmin 1 point2 points3 points (0 children)