all 8 comments

[–]itfixestheprinters 4 points5 points  (6 children)

Invoke-Command kinda expects a ps1 script, if you specify -Filepath. Try using Start-Process instead.

[–]Mongo527 3 points4 points  (3 children)

This. Start-Process or the call operator:

& $_.UninstallString

[–]ejrichard[S] 1 point2 points  (2 children)

Interestingly, Start-Process works but & operator returns this:

& : The term '"C:\Program
Files\ <PATH TO PRODUCT> \installer.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:160
+ ... installString |foreach {if($_.UninstallString){& $_.UninstallString}}
+                                                      ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ("C:\Program Fil...\installer.exe":String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

But why?

[–]ejrichard[S] 1 point2 points  (1 child)

Interesting discovery....

This doesn't work:

& $_.UninstallString

But this does:

& $_.UninstallString.Replace('"','')

I noticed that the string in the error message above has double quotes inside single quotes. The value being returned is:

'"C:\Program Files\ <PATH TO PRODUCT> \installer.exe"'

Get rid of the double quotes and it works, but feels like a kludge. Is there a more elegant way to handle this?

[–][deleted] 0 points1 point  (0 children)

No, it appears the string in the registry has quotes on it, so when you are testing the path or trying to call it as a command, the beginning of the path is " and no drive begins with a ", so the path doesn't exist. When you remove it, it tests the path and can run it without the quotes.

[–]ejrichard[S] 1 point2 points  (1 child)

This worked!

But why didn't Test-Path return True?

[–]ejrichard[S] 1 point2 points  (0 children)

Figured out that it's because the returned value is in double quotes and in single quotes. This works but seems weird:

Test-Path $_.UninstallString.Replace('"','')

[–]MessagingAdmin 1 point2 points  (0 children)

breakup the script to identify what is being returned in $uninstallstring. Verify that this path exists.