I have an uninstall string like MsiExec.exe /I{370E1F16-ED05-4958-A820-78CD32DD70D8} and want the text inside the braces. My pattern is '(?<={).+?(?=})', but my regex call doesn't return anything. What am I missing?
How do I extract text between curly braces in PowerShell?
Answers
Your pattern works for that example. The call needs a method name after [regex]::, so use [regex]::Match($uninstallString, $pattern).Value.
The lookbehind and lookahead check for the braces without including them in the result. Match returns the first match. If there is no match, its Value is an empty string, so you can check the match object's Success property when that distinction matters.
Reply to this answer
You could skip regex if the format is predictable: $uninstallString.Split([char[]]'{}', 3)[1]. That splits on either brace and takes the piece after the opening one.
This assumes an opening brace is present and the value you want is in the expected position. It won't validate that the braces are paired or that the contents are a GUID.
Reply to this answer
One wrinkle if this is for an uninstall: the extracted value has no braces. You may need to put them back around an MSI product code, and /I is not the uninstall switch. Don't assume every registry uninstall string is an MSI command either. Check the format before building a command from it.
Reply to this answer
If your registry query can find more than one JDK, process each entry separately rather than treating all the UninstallString values as one string. It also helps to inspect the selected entries before extracting anything, especially when a match is missing or an uninstall command has a different format.
Reply to this answer
For a broader cleanup job, PowerShell Application Deployment Toolkit is another option worth looking at. It may save you from building package removal logic around individual registry strings, though you should still check what it will remove before running it.
Reply to this answer
Add your answer
Answers are reviewed before publication.
r.529~plovrani:
The [char[]] cast makes the two braces separate delimiter characters. The 3 limits the result to at most three pieces.