Getting RegKey Info and Formatting

Hi everyone,

long time user, first time poster :slight_smile: I’m currently working on an uninstall app for Sophos Endpiont Defense and I’m stuck at a particular point. As there can be many features in any given Sophos installation, I need to find all installed products and their MSI product code AND their uninstall string, if there is one. That’s not a problem, I use the following code to do that (in a for/each loop where $Product is the DisplayName of the Sophos product, which is known):

$Reg64MSIProductCode = Get-ItemProperty "$UninstallRegPath64\*" | Where-Object -Property DisplayName -Like "$Product" | Select-Object PSChildName,UninstallString,QuietUninstallString

The problem I’m facing, that I can’t seem to solve, is the following: PSChildName is the MSI product code which works fine, if the product can be uninstalled via MSI. However, if I use the UninstallString or QuietUninstallString, the App Deployment Toolkit fails with an error in the function “Execute-Process”. This seems to be due o the fact, that both strings have quotation marks in them and these are of course retrieved when retrieving the string, i.e. “C:\Program Files\Notepad++\uninstall.exe” /S (I’ll use Notepad++ as an example here) for the QuietUninstallString. I go ahead and split the string at / in order to seperate the path and parameters. So far so good. My line is then

Execute-Process -Path $UninstallString -Parameters "/$Parameters" -WindowStyle 'Hidden' -Wait -ErrorAction Stop

Then I’ve tried using trim(‘"’,’ '), split, etc. on $UninstallString in order to get rid of the quotation marks and the ending space that occurs after the initial split. When I do this manually in the console, it works. If I do it in the script, it doesn’t. If I define the path as a variable and enter the path manually, i.e. $UninstallString = “C:\Program Files\Notepad++\uninstall.exe”, the function works fine and the product in uninstalled without problems. My line is then:

Execute-Process -Path "$UninstallString" -Parameters "/$Parameters" -WindowStyle 'Hidden' -Wait -ErrorAction Stop

If I use the same line (with “” around $UninstallString) without manually defining the variable, the function fails aswell (just wanted to clarify in case someone points out the “” are missing in the first Execute-Process line).

So I guess my question is - does anybody know how to properly format the result I get from the registry in order for Execute-Process to properly work?

Cheers,
Fred

I had the chance to get the help of one of our developers and we found the error. Posting incase anyone has the same problem.
As per examples found on Stackoverflow and others, I was trimming the string as such:

$UninstallString.Trim('"','')

This leads to the trim being done, but it’s not automatically saved to the variable again. Based on the examples I found, I was under the impression it would automatically be saved. I then opted for replace instead of trim and wound up with a functioning line and the entire script ran fine.

$UninstallString = $UninstallString.Replace('"','')

Cheers,
Fred

1 Like