0

I am trying to fetch the matching strings from a variable.

For example,

$out = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\{F1E1501C-B95C-42E0-BFD4-757DF1B961D1}"

I need the value inside "{ }".

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
jerry
  • 45
  • 1
  • 5
  • 3
    What have you tried so far and why hasn't it worked as needed? Do you know you could use regular expressions to get the desired result? – Olaf Jun 25 '18 at 06:16
  • 3
    ... or `-split` or ... – Lieven Keersmaekers Jun 25 '18 at 06:16
  • @Olaf I am new to power shell, I have googled so far, how to use regular expression, can you explain $out = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | select-string -pattern "zTPFGI" @{UninstallString=RunDll32 C:\PROGRA~2\COMMON~1\INSTAL~1\engine\6\INTEL3~1\Ctor.dll,LaunchSetup "C:\Program Files (x86)\InstallShield Installation Information\{F1E1501C-B95C-42E0-BFD4-757DF1B961D1}\setup.exe" -l0x9 FromAddRemove; Display Name=zTPFGI; LogFile=C:\Program Files (x86)\InstallShield Installation Information\{F1E1501C-B95C-42E0-BFD4-757DF1B961D1}\setup.ilg} – jerry Jun 25 '18 at 07:04
  • @LievenKeersmaekers – jerry Jun 25 '18 at 07:05
  • 2
    @jerry You should add this explanation to your actual question. There it would even possible to format it nicely. – Olaf Jun 25 '18 at 07:40

1 Answers1

3

You can use a RegEx for this:

$out = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall{F1E1501C-B95C-42E0-BFD4-757DF1B961D1}"

$out -match "^.*{(?<guid>.*)}$" | Out-Null

You can then access the value like this:

$matches.Guid

-match produces a bool that lets you know if it was successful or not. Here I discard it by sending it to Out-Null, but you can use it to decide if you should proceeed, by, say, wrapping it in an if:

if ($out -match "^.*{(?<guid>.*)}$")
{
   # Do something
}

Note that you can get a more accurate match for the GUID using the patterns mentioned here:

RegEx for GUID

boxdog
  • 7,894
  • 2
  • 18
  • 27
  • $out = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | select-string -pattern "zTPFGI" i dont know the GUID i have used the above command to get the GUID – jerry Jun 25 '18 at 07:22
  • 2
    @jerry Please re-read the answer. `(?...)` is regular expression syntax for a named capturing group, so you can access the group by name (`$matches['foo']` or `$matches.foo`) rather than by index (`$matches[1]`). – Ansgar Wiechers Jun 25 '18 at 08:04