# Extract Variable Values from String

**URL:** <https://forums.powershell.org/t/extract-variable-values-from-string/19105>\
**Category:** PowerShell Help\
**Created:** [April 17, 2022, 6:21am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105 "2022-04-17T06:21:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TXTOM](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@TXTOM](https://forums.powershell.org/u/TXTOM)\
**Post date:** [April 17, 2022, 6:21am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/1 "2022-04-17T06:21:10Z")

</div>

Looking for an elegant way to extract values out of a few strings:  
Sorry this is super messy, but I’m just trying to make it all work

Blockquote

$FileNameA = “C:\ADBackup\GPOs{2C197968-849A-4A6E-B2CB-2715357DF2C5}\Machine\Preferences\Groups\Groups.xml”

#REM GPGuid is the value between the { } brackets from $FileNameA  
#REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml  
$GPGuidA = 2C197968-849A-4A6E-B2CB-2715357DF2C5  
$GPKeyA = Machine\Preferences\Groups  
###################################################################################

$FileNameB = “C:\ADBackup\GPOs{7AF2FC2F-7A88-47BB-9F61-BEE0C929DF6E}\User\Preferences\Drives\Drives.xml”

#REM GPGuid is the value between the { } brackets from $FileNameB  
#REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Drives.xml

> Blockquote

I got it to partially work, but it’s super sloppy.

> Blockquote

| | $Temp = { |
| --- | --- |
| | $pos = $name.IndexOf($Temp) |
| | $leftPart = $name.Substring(0, $pos) |
| | $rightPart = $name.Substring($pos+1) |
| | $Temp = } |
| | $pos = $rightpart.IndexOf($Temp) |
| | $GPOGuid = $rightpart.Substring(0, $pos) |
| | $Temp = } |
| | $pos = $name.IndexOf($Temp) |
| | $GPOPath = $name.Substring($pos+2) |
| | $Temp = | |
| | $pos = $name.IndexOf($Temp) |
| | $GPOXML = $name.Substring($pos+1) ###Not working |

Blockquote

Next I want to read in the contents of the .XML files and get data out of them:

Contents of Groups.xml  
#This GPO adds MyCorp\Office365\_Containers to a local server group named FSLogix ODFC Include List and adds MyCorp\Profile\_Containers to a local server group named FSLogix Profile Include List

\<?xml version="1.0" encoding="UTF-8"?\>[](%5Cl%20%22%22)[](%5Cl%20%22%22)

Contents of Drives.xml  
#REM This GPO maps 3 drive letters

\<?xml version="1.0" encoding="UTF-8"?\>[](%5Cl%20%22%22)[](%5Cl%20%22%22)[](%5Cl%20%22%22)

##Attaching DrivesXML.jpg

 ![DrivesXML](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/e/ed855e14f99ab7809e1af1636967abebb1478cd9.jpeg)  
file so formatting is better

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [April 17, 2022, 9:15am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/2 "2022-04-17T09:15:24Z")

</div>

hey,

Not sure I understand but i think you can use regular expressions to get those values.

I searched on google how to find value between two strings or exclude string etc and had a go.

[find value between brackets](https://regex101.com/r/iNIsNo/1)

[value after } but does not include the last \ on the right or data after](https://regex101.com/r/Q1j8w8/1) this one is messy 😄

i might be completely off with my approach lol

```auto
# REM GPGuid is the value between the { } brackets from $FileNameA
[regex]$rxGuid = '(?<={).*(?=})'

# REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml
[regex]$rxKey = '(?<=}\\)(.*)(?:(?![^\\])\\)'

$FileNameA = “C:\ADBackup\GPOs{2C197968-849A-4A6E-B2CB-2715357DF2C5}\Machine\Preferences\Groups\Groups.xml”
$FileNameB = “C:\ADBackup\GPOs{7AF2FC2F-7A88-47BB-9F61-BEE0C929DF6E}\User\Preferences\Drives\Drives.xml”

$GPGuid = $rxGuid.Matches($FileNameA).value
$GPKey = $rxKey.Matches($FileNameA).groups[1].value

```

try to format your code, there’s a guide how to do it on the forum. for the second part you can check online/this forum how to extract data from xml file or attach sample xml and we can try to test

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 17, 2022, 10:19am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/3 "2022-04-17T10:19:21Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

An image of code or XML sample data is not helpful at all.

PowerShell is able to work with XML data. So if I got it right you don’t need regular expressions at all.

> **[Mastering everyday XML tasks in PowerShell](https://powershellmagazine.com/2013/08/19/mastering-everyday-xml-tasks-in-powershell/)**
>
> PowerShell has awesome XML support. It is not obvious at first, but with a little help from your friends here at PowerShellMagazine.

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [April 17, 2022, 6:05pm UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/4 "2022-04-17T18:05:25Z")

</div>

ah apologies I assumed user was referring to filename in string and thought xml part was something different.

---

<div class="post-metadata">

**Author:** ![TXTOM](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@TXTOM](https://forums.powershell.org/u/TXTOM)\
**Post date:** [April 18, 2022, 12:06am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/5 "2022-04-18T00:06:18Z")

</div>

You’re example was perfect and I got it to work. Greatly appreciated!!

```auto

$FileName = '*.xml' #you can use wildcards here for name and for extension
$searchinfolder = $ADBackup + "\GPOs"
$Report = Get-ChildItem -Path $searchinfolder -Filter $Filename -Recurse | %{$_.FullName}
$Report

     Foreach ($UserGPO in $Report) {
        # REM GPGuid is the value between the { } brackets from $UserGPO
        [regex]$rxGuid = '(?<={).*(?=})'
        # REM GPKey is the after } but does not include the last \ on the right or data after, basically chop off \Groups.xml
        [regex]$rxKey = '(?<=}\\)(.*)(?:(?![^\\])\\)'
        $GPGuid = $rxGuid.Matches($UserGPO).value
	$GPKeyPrefix = "HKCU\"
        $GPKey = $rxKey.Matches($UserGPO).groups[1].value
        $GPOGUIDS = (Get-Content -Path 'C:\ADBackup\GPOGUIDs.csv' | Where-Object {$_.gpoGuid -eq $GPGuid})
        $GPName = (Import-csv 'C:\ADBackup\GPOGUIDs.csv' | where {$_.gpoGuid -eq $GPGuid}).DisplayName
	$GPName = "GPOTest" ###Remove this line later using temp value here
	$GPValueName = "Temp" ###Read these values from XML file###
	$GPType = "String" ###Read these values from XML file###
	$GPValue = "ABC123" ###Read these values from XML file###

        $Test = "Set-GPRegistryValue -Name $GPName"
        $Test = $Test + " -Key $GPKeyPrefix$GPKey"
        $Test = $Test + " -ValueName $GPValueName"
        $Test = $Test + " -Type $GPType"
        $Test = $Test + " -Value $GPValue"
        Write-Host "$Test"

     }

```

Output:  
Set-GPRegistryValue -Name GPOTest -Key HKCU\User\Preferences\Drives -ValueName Temp -Type String -Value ABC123

---

<div class="post-metadata">

**Author:** ![TXTOM](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@TXTOM](https://forums.powershell.org/u/TXTOM)\
**Post date:** [April 18, 2022, 12:21am UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/6 "2022-04-18T00:21:43Z")

</div>

Sorry about the XML before. I wasn’t able to upload it. I manually formatted it this time around and hope it’s useful.

```auto
<?xml version="1.0" encoding="UTF-8"?>
   <Drives clsid="{8FDDCC1A-0C3C-43cd-A6B4-71A6DF20DA8C}">
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" bypassErrors="1" uid="{73F410F7-7539-49E0-A284-E2FEBCE334CD}" changed="2021-05-10 05:34:52" image="0" status="F:" name="F:">
         <Properties letter="F" useLetter="1" persistent="1" label="" path="\\phcfs01\fapps" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
         </Drive>
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" uid="{94F985A1-1E6E-4ECD-AAF2-57E7592D149D}" changed="2021-05-10 05:35:32" image="0" status="Q:" name="Q:">
         <Properties letter="Q" useLetter="1" persistent="1" label="" path="\\phcfs01\nsd" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
      </Drive>
      <Drive clsid="{935D1B74-9CB8-4e3c-9914-7DD559B7A417}" uid="{D38E3EFB-29E2-4CA2-B21C-E70752581020}" changed="2021-05-10 05:36:06" image="0" status="R:" name="R:">
         <Properties letter="R" useLetter="1" persistent="1" label="" path="\\phcfs01\rapps" userName="" allDrives="NOCHANGE" thisDrive="SHOW" action="C"/>
      </Drive>
   </Drives>

```

Even if I import the values from the XML file I’m not sure how to convert it to something the USER GPO will take. It appears USER GPO settings are stored in HKCU in the registry. However, the value DRIVE as in Drive Mappings ends up in HKCU\Network. Please refer to below copy of registry keys. I’ve been using installed Poweshell module GPRegistryPolicy to get most of this working. Computer GPO settings export to CSV and can be imported on a different system easily, but User is still not working.

Windows Registry Editor Version 5.00

[HKEY\_CURRENT\_USER\Network]

[HKEY\_CURRENT\_USER\Network\f]  
“RemotePath”=“\\phcfs01\fapps”  
“UserName”=dword:00000000  
“ProviderName”=“Microsoft Windows Network”  
“ProviderType”=dword:00020000  
“ConnectionType”=dword:00000001  
“ConnectFlags”=dword:00000000  
“DeferFlags”=dword:00000004

[HKEY\_CURRENT\_USER\Network\q]  
“RemotePath”=“\\phcfs01\nsd”  
“UserName”=dword:00000000  
“ProviderName”=“Microsoft Windows Network”  
“ProviderType”=dword:00020000  
“ConnectionType”=dword:00000001  
“ConnectFlags”=dword:00000000  
“DeferFlags”=dword:00000004

[HKEY\_CURRENT\_USER\Network\r]  
“RemotePath”=“\\phcfs01\rapps”  
“UserName”=dword:00000000  
“ProviderName”=“Microsoft Windows Network”  
“ProviderType”=dword:00020000  
“ConnectionType”=dword:00000001  
“ConnectFlags”=dword:00000000  
“DeferFlags”=dword:00000004

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 18, 2022, 12:06pm UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/7 "2022-04-18T12:06:19Z")

</div>

> [@TXTOM](#):
>
> Even if I import the values from the XML file I’m not sure how to convert it to something the USER GPO will take. It appears USER GPO settings are stored in HKCU in the registry.

You might explain what you actually want to do. Not the way you think you should go to accomplish what you want - the bigger picture. 😉 There might be a better way or at least we might be able to tell you when you’re on the wrong path. 😉

---

<div class="post-metadata">

**Author:** ![TXTOM](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@TXTOM](https://forums.powershell.org/u/TXTOM)\
**Post date:** [April 18, 2022, 6:34pm UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/8 "2022-04-18T18:34:50Z")

</div>

Hi Olaf,

The ultimate goal is to be able to export Computer and User GPO settings so they can be imported in to a different system. Different system means a new Active Directory Domain that is independent of the source domain. I have certain system settings like Event Log sizes or firewall exceptions that I used to deploy by hand. I’ve completed Computer GPO’s, where I can export them to a CSV file and easily import them to a new system. It’s essentially automation so I don’t make a mistake entering it manually. User GPO’s are more difficult. For the time being I’ve created a ZIP file of the \domain\sysvol\domain\policies\*.xml including the full path so I can at some point import the data in to the GPO. I’m attaching a screen shot of a GPO User Configuration settings where it maps an F: drive and Q: drive.

 ![UserGPO](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bbca569eaa9aed85f39c517d0f16faa31f809922.jpeg)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 18, 2022, 6:53pm UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/9 "2022-04-18T18:53:07Z")

</div>

It feels a little bit like you’re re-inventing the wheel again. Most of the times someone else already accomplished what you need and shared it with the world. You just have to find the right source …

I googled “Export GPO Import GPO” and found some promissing sites:

> **[Export a GPO to a File - Microsoft Desktop Optimization Pack](https://docs.microsoft.com/en-us/microsoft-desktop-optimization-pack/agpm/export-a-gpo-to-a-file)**
>
> Export a GPO to a File

> **[Using PowerShell to Export GPOs for Fancy GPO Report](https://adamtheautomator.com/powershell-export-gpo/)**
>
> Learn how to import the GPO module in PowerShell, use PowerShell to export GPOs, and get GPOs linked to an OU to build some fancy reports.

> **[rakhesh sasidharan's mostly techie somewhat purpley blog](https://rakhesh.com/windows/export-and-import-group-policy-objects-gpos/)**
>
> Blog

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/extract-variable-values-from-string/19105/10 "2024-05-16T20:18:01Z")

</div>


