r/PowerShell • u/anonymousITCoward • 11d ago
Question RegEx help...
I have never been able to wrap my head around regex... I have no idea why... anyways... I'm just trying to get the string between 2 braces (curly brackets). Here's what I've mangled together googling up... One of the many regex checking tools online said this should work but.. it doesn't
$uninstallString = (Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object { $_.DisplayName -like "*jdk*" }).UninstalLString$uninstallString = (Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object { $_.DisplayName -like "*jdk*" }).UninstalLString
$pattern = '(?<={).+?(?=})'
[Regex]::($uninstallString,$pattern).Value
<#
$uninstallString outputs the below
MsiExec.exe /I{370E1F16-ED05-4958-A820-78CD32DD70D8}
#>The above code actually outputs nothing...
My head is so frazzled I'm not sure where to begin with it
Any help, or a point in the right direction would be appreciated.
20
Upvotes
26
u/SysAdminDennyBob 11d ago edited 11d ago
If you are trying to uninstall multiple flavors of Java with powershell I highly suggest you not reinvent the wheel here. You can do about four tiny lines of code and remove 483 different JREs and JDKs if you take a peek at the Powershell Application Deployment Toolkit. It is amazing for this task.
PSAppDeployToolkit
edit: also Regex LOL
5
u/realslacker 11d ago edited 11d ago
If you know there is only one guid patten you can simplify by using [string]::Split...
This will do what you were originally trying to do:
8
u/BrettStah 11d ago
These days for regex help, I just throw my question to an LLM. They understand it really well!
3
u/BlackV 11d ago edited 11d ago
You are trying to do too much on 1 command line, break it down to bits first, and use your properties rather than flat objects
that is the best way to understand it
also you dont need the
Get-ChildItemon top of theGet-ItemPropertyas it also supports the-pathparameterWhen you are building it I'd do the following
Get all your keys first, note I am looking at the 64bit key AND the 32bit Key (you are not), this can be especially painful via an RMM tools that might run as a 32bit process (I've also ignored current user)
then you can filter
what does that return?
or build a custom object for cleaner validation maybe
Filtering
Is it a single object or multiple?
Then depending on how complex you want to go
putting it all to variables let you test over and over to get it how you want
Also note the
parentpath should be the sameuninstall guidas the installer if its a MSI install, which could save all your string manipulation (I think i've used that when bad apps dont publish the uninstall string)2
u/PinchesTheCrab 11d ago
I don't think regex is the path of least resistance here. Does this work?
For a regex approach there's a ton of different ways you could do it, here's two:
12
u/crypticsage 11d ago
regex101.com
Has a section to add test strings
Provides info what each entry does.
1
u/the_lazy_sysadmin 11d ago
so, the thing with pulling the 'uninstall string' from the registry, is sometimes it will be thrown in there with the /I switch listed, and not the /x switch (which is for uninstalling with msiexec). The entries placed in there are whatever the devs of whatever application decided to throw in there. Another example would be some Dell bloatware, which will often have a quiet uninstall string listed in the registry, conveniently omits a 'no reboot' option, which does in fact exist, if you look up Dell's documentation and test it.
What you can do instead, for msi packages, is pull the LocalPackage property. You can feed that into a variable, and slap that in a line calling msiexec.exe /x $variablename, along with whatever msiexec.exe options you'd want.
1
u/purplemonkeymad 11d ago
Have you actually assigned the result to a value? If I do:
It outputs the guid, so I think your regex does what you think. How are you handling the results?
1
u/LowDark9328 11d ago
I like replace with a regex the matches the whole string "^....$" and named groups.
1
u/Modify- 11d ago
Not sure about Java, but something like this works fairly well.
1
u/uptimefordays 11d ago edited 11d ago
Your regex is actually fine! The problem is the line calling it.
[Regex]::(...)is missing the method name, so it should be:Or the more PowerShell-y way, using
-match. Keep the braces in the match, sincemsiexec /xneeds them (thanks for testing that):That said, you don't need to parse the string at all. For MSI installs, the registry key name under
Uninstallis the product code, braces included, andGet-ItemPropertygives it to you asPSChildName:This also covers a few things that would bite you later:
$uninstallStringbecomes an array, and the regex quietly returns only the first match. The pipeline above gives you one object per install.WOW6432Node.WindowsInstaller -eq 1skips entries whose key name isn't a product code./I(modify/repair) instead of/x, or leave out switches like no-reboot. Building the command yourself avoids both.To uninstall, swap the
Select-Objectfor:Edit: fixed the
-matchexample to keep the braces, and added theWindowsInstallerfilter,/norestart, and exit codes.1
u/KevMar Community Blogger 4d ago
I am really lazy with my regex. I
()sub expression the part I want, then?<name>give it a name, finally.*or.+greedy match everything.Then pull out the named match off of $matches.
That greedy match works for 95% of my use-cases (where the rest of the line is fairly static).
-2
u/Incrediblecodeman 11d ago
Dude just ask ai wtf?
Typewriter ahh comment .