Extract exact text from string

I have a script that will get the UninstallString from the registry. Example:

UninstallString

MsiExec.exe /X{E57AF6DD-7B88-49F1-CG54-3CE1A2F34CA9}

I’ve tried ChatGPT but I must be asking the question wrong. How do I get just the uninstall string including brackets?

i.e.

{E57AF6DD-7B88-49F1-CG54-3CE1A2F34CA9}

Thank you.

You’ve only asked ChatGPT? What about google or bing? I find it hard to believe you’re not able to find ANY helpful information. I’m curious what question(s) you have asked it that gave you nothing even close.

ME: extract guid from uninstall string using powershell

ChatGPT:
You can extract the GUID from an uninstall string using PowerShell by using regular expressions. The uninstall string typically contains the path to the uninstaller executable along with the GUID, which is enclosed in curly braces {}. Here's a PowerShell script to extract the GUID:

powershell
Copy code
# Define the uninstall string
$uninstallString = 'C:\Program Files\Example\Uninstall.exe /GUID={12345678-1234-5678-1234-567812345678}'

# Use regular expressions to extract the GUID
$match = [regex]::Match($uninstallString, '\{[A-F0-9-]+\}')

# Check if a match was found
if ($match.Success) {
    # Extract the GUID from the match
    $guid = $match.Value
    Write-Host "Extracted GUID: $guid"
} else {
    Write-Host "No GUID found in the uninstall string."
}
In this script:

We define the uninstall string ($uninstallString) as an example.
We use the [regex]::Match method to search for a pattern within the uninstall string. The pattern '\{[A-F0-9-]+\}' looks for a sequence of characters enclosed in curly braces {} consisting of uppercase letters A-F, digits 0-9, and hyphens.
If a match is found (i.e., if $match.Success is true), we extract the matched GUID from $match.Value and display it.
You can replace the $uninstallString variable with the actual uninstall string you want to extract the GUID from.
1 Like

Why don’t you share your code? So we might be able to tell what’s wrong with it. And what’s the actual purpose of your script?

And just to have it mentioned at least once: Not all uninstall strings you’ll find in the registry actually trigger a de-installation of an app. Some only launch the setup interactivly.

1 Like

That’s a great point. Some do the same as “change” in the old add/remove programs applet.

1 Like

$GetUninstallString = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
ForEach-Object {Get-ItemProperty $.pspath} | where-object {$.DisplayName -like ‘Acrobat’ } | Select-Object UninstallString

all I want is the two brackets {} and the data inbetween the {the two brackets}

thank you.

Do you actually read your own posts when you posted them?

Please … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Is it that what you’re looking for?

$UnistallPathList = 
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'

$AcrobatUninstallString = 
Get-ChildItem -Path $UnistallPathList |
ForEach-Object {Get-ItemProperty $_.pspath} | 
where-object {$_.DisplayName -like '*Acrobat*'} | 
Select-Object -ExpandProperty UninstallString

$AcrobatUninstallString -match '\{.*\}'
$Matches[0]

Did you read my response? It literally showed you a way to get exactly what you want.

# Define the uninstall string
$uninstallString = 'C:\Program Files\Example\Uninstall.exe /GUID={12345678-1234-5678-1234-567812345678}'

# Use regular expressions to extract the GUID
$match = [regex]::Match($uninstallString, '\{[A-F0-9-]+\}')

# Check if a match was found
if ($match.Success) {
    # Extract the GUID from the match
    $guid = $match.Value
    Write-Host "Extracted GUID: $guid"
} else {
    Write-Host "No GUID found in the uninstall string."
}

Very elegant !! Thank you, that is exactly what I was looking for.