# Bulk create variables from XmlLinkedNode

**URL:** <https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757>\
**Category:** PowerShell Help\
**Created:** [October 27, 2022, 5:50pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757 "2022-10-27T17:50:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 27, 2022, 5:50pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/1 "2022-10-27T17:50:08Z")

</div>

Hello !  
I need some help to bulk create variables from an xml file content. Let me explain by powershell.  
I’m executing the following code:

```auto
[xml]$XmlLTI = Get-Content C:\Users\AdminDell\Desktop\HOLDING\LTI-HOLDING-v0\bin\x64\Settings.xml
$XmlLTI.countries.country | Where-Object CountryDisplayName -eq 'China'
$test = $XmlLTI.countries.country | Where-Object CountryDisplayName -eq 'China'
$test

```

The result is :

```auto
CountryCode : CHN-W10
ADDomainNetbiosName : CHINA
ADDomainFQDN : xxx
ADOrgUnit : AdOrgUnit
ADJoinDomainUser : user1
...

```

The question is : how to dynamically create variables foreach element under $Test.xxx (the name and count of elements can change) ?

- $CountryCode = CHN-W10
- $ADDomainNetbiosName = CHINA
- …

Thank you \<3

---

<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:** [October 27, 2022, 7:06pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/2 "2022-10-27T19:06:04Z")

</div>

GiTong,  
Welcome to the forum. 👋🏽

> [@GiTong](#):
>
> bulk create variables

> [@GiTong](#):
>
> dynamically create variables

In the vast majority of the cases that is unnecessary in PowerShell and indicates a missunderstanding of the way PowerShell works.

To answer your question anyway - you would use a loop. 😉

Either with

> **[about Foreach - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.2)**
>
> Describes a language command you can use to traverse all the items in a collection of items.

or

> **[ForEach-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2)**
>
> The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the cmdlet or specified using the InputObject parameter. Starting in Windows PowerShell 3.0, there are two different ways...

You can create new variables you don’t know the name of yet with

> **[New-Variable (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/de-de/powershell/module/microsoft.powershell.utility/new-variable?view=powershell-7.2)**
>
> Das New-Variable Cmdlet erstellt eine neue Variable in PowerShell. Sie können der Variablen beim Erstellen einen Wert zuweisen oder den Wert zuweisen bzw. ändern, nachdem die Variable erstellt wurde. Sie können die Parameter New-Variable verwenden,...

You may elaborate a liitle more detailed about what you’re actually want to do and we might be able to help you step further.

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 27, 2022, 8:33pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/3 "2022-10-27T20:33:33Z")

</div>

Thank you for your help. More context: i have a graphical interface with a combobox, the user choose a country. The script reads the xml, retrieve the attributes for this country, then display it to the next screen and do lot of others tasks (domain join, registration …) it’s why i need these variables to be created.

Thanks for the foreach-object that’s the trick , but i don’t know how to separate the “left” attribute and the right “value”  
example :

```auto
$test | ForEach-Object {$_}
CountryDisplayName : China

```

---

<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:** [October 27, 2022, 9:38pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/4 "2022-10-27T21:38:55Z")

</div>

> [@GiTong](#):
>
> it’s why i need these variables to be created.

I’m still not convinced that you need to create variables since PowerShell creates loop variables automatically when you use a loop.

> [@GiTong](#):
>
> i don’t know how to separate the “left” attribute and the right “value”

In PowerShell we work with objects and properties. So if you search for you should search for “properties” - not “left attributes” 😉

We use the so called dot notation to access the properties of an object.

So it’s either this:

```auto
$test | 
    ForEach-Object {
        $_.CountryDisplayName
    }

```

or this:

```auto
foreach ($element in $test) {
    $element.CountryDisplayName
}

```

The latter one is much faster most of the times. 😉

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 27, 2022, 10:49pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/5 "2022-10-27T22:49:30Z")

</div>

> [@Olaf](#):
>
> ```auto
> $test | 
> ForEach-Object {
> $_.CountryDisplayName
> }
> 
> ```

Sorry, i think my request is not clearly understood. I would like to create as many variables as the number of properties in my object $Test.  
More clear: $Test (which is a xmlview type) has multiple properties

- $Test.CountryCode is CHN-W10
- $Test.ADDomainNetbiosName : CHINA
- …  
i want to find all the existing properties for $Test (this is not a fixed number and properties names) , then create the variable $CountryCode, ADDomainNetbiosName , and store the value in this variable 🙂

---

<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:** [October 27, 2022, 11:02pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/6 "2022-10-27T23:02:34Z")

</div>

> [@GiTong](#):
>
> i think my request is not clearly understood.

Hmmm … and I think you didn’t understand what I was trying to explain. In 99.99% of the cases that’s not necessary.

> [@GiTong](#):
>
> i want to find all the existing properties for $Test (this is not a fixed number and properties names)

I’ve got this.

> [@GiTong](#):
>
> then create the variable $CountryCode, ADDomainNetbiosName , and store the value in this variable

Even if I’m still not convinced that this is needed I already said what you need - a loop and `New-Variable`

Please share some sanitized sample data of your XML file (formatted as code, please) and explain what you want to do with the variables you think you need to create.

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 28, 2022, 8:48am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/7 "2022-10-28T08:48:33Z")

</div>

> [@Olaf](#):
>
> sanitized sample data of your XML file

Here it is :

```auto
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
    <CountryDisplayName>China</CountryDisplayName>
    <CountryCode>CHN-W10</CountryCode>
    <ADDomainNetbiosName>CHINA</ADDomainNetbiosName>
    <OGID>CHN-W10</OGID>
    <StagingUser>stagingUser1</StagingUser>
    <Regex></Regex>
</country>
<country>
    <CountryDisplayName>Honk-Kong</CountryDisplayName>
    <CountryCode>HKG-W10</CountryCode>
    <ADDomainNetbiosName>HONGKONG</ADDomainNetbiosName>
    <OGID>WS1OGID>HKG-W10</OGID>
    <StagingUser>stagingUser2</StagingUser>
    <Regex>123456789</Regex>
	<VarXYZ>abcdef</VarXYZ>
	<VarABC>xyz</VarABC>
</country>
</countries>

```

I put more info below .

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 28, 2022, 8:57am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/8 "2022-10-28T08:57:11Z")

</div>

Here is one part of the xml.  
I need a function that will create the variables depending the CountryDisplaName.

Set-VariablesFromXML -xml $xml -country “China” \> creates all variables in the China node ($CountryDisplaName, $CountryCode … with their repective values China, CHN-W10 …)  
Each node can have more or less variables.

```auto
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
    <CountryDisplayName>China</CountryDisplayName>
    <CountryCode>CHN-W10</CountryCode>
    <ADDomainNetbiosName>CHINA</ADDomainNetbiosName>
    <OGID>CHN-W10</OGID>
    <StagingUser>stagingUser1</StagingUser>
    <Regex></Regex>
</country>
<country>
    <CountryDisplayName>Honk-Kong</CountryDisplayName>
    <CountryCode>HKG-W10</CountryCode>
    <ADDomainNetbiosName>HONGKONG</ADDomainNetbiosName>
    <OGID>WS1OGID>HKG-W10</OGID>
    <StagingUser>stagingUser2</StagingUser>
    <Regex>123456789</Regex>
	<VarXYZ>abcdef</VarXYZ>
	<VarABC>xyz</VarABC>
</country>
</countries>

```

---

<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:** [October 28, 2022, 11:43am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/9 "2022-10-28T11:43:19Z")

</div>

> [@GiTong](#):
>
> I need a function that will create the variables depending the CountryDisplaName.

You still don’t say whatfor you need all these variables.

Here is how to extract the node names:

```auto
[XML]$XMLInput = 
@'
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
    <CountryDisplayName>China</CountryDisplayName>
    <CountryCode>CHN-W10</CountryCode>
    <ADDomainNetbiosName>CHINA</ADDomainNetbiosName>
    <OGID>CHN-W10</OGID>
    <StagingUser>stagingUser1</StagingUser>
    <Regex></Regex>
</country>
<country>
    <CountryDisplayName>Honk-Kong</CountryDisplayName>
    <CountryCode>HKG-W10</CountryCode>
    <ADDomainNetbiosName>HONGKONG</ADDomainNetbiosName>
    <OGID>WS1OGID>HKG-W10</OGID>
    <StagingUser>stagingUser2</StagingUser>
    <Regex>123456789</Regex>
	<VarXYZ>abcdef</VarXYZ>
	<VarABC>xyz</VarABC>
</country>
</countries>
'@ 

$XMLInput.countries.country[0].psobject.BaseObject | 
Get-Member | 
Where-Object -Property MemberType -EQ -Value Property | 
Select-Object -Property Name

```

This outputs the node names of the first `country` node. For the second one you use `[1]` and so on. And for all of them you use a loop. 😉

THe output for the first is this:

```auto
Name
----
ADDomainNetbiosName
CountryCode
CountryDisplayName
OGID
Regex
StagingUser

```

… and for the second node it’s this:

```auto
Name
----
ADDomainNetbiosName
CountryCode
CountryDisplayName
OGID
Regex
StagingUser
VarABC
VarXYZ

```

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [October 28, 2022, 11:51am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/10 "2022-10-28T11:51:04Z")

</div>

Just to put my 2 pennies in:

```auto
$XMLInput.Countries.Country

```

Will also give you what you are looking for (using Olaf’s example).  
You can then do things like:

```auto
$XMLInput.Countries.Country[0]

```

To give you the first element and all its properties.  
You don’t need to separate them out into separate variables, they can be happily nested.

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 28, 2022, 12:34pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/11 "2022-10-28T12:34:44Z")

</div>

> `You still don’t say whatfor you need all these variables.`  
> i thought my answer was clear, do you need more details ?  
> `> The script reads the xml, retrieve the attributes for this country, then display it to the next screen and do lot of others tasks (domain join, registration …) it’s why i need these variables to be created.`

---

<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:** [October 28, 2022, 1:04pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/12 "2022-10-28T13:04:24Z")

</div>

> [@GiTong](#):
>
> i thought my answer was clear, do you need more details ?

I think you missunderstood.

> [@GiTong](#):
>
> The script reads the xml, retrieve the attributes for this country, then display it to the next screen and do lot of others tasks

You may show your code. You don’t need to create variables to display the properties and / or the values. You can use the existing properties and values of the instance of the objects created by PowerShell from your input XML.

One issue with programmaticall created variables with arbitrary names is to enumerate them later on.

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 28, 2022, 1:54pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/13 "2022-10-28T13:54:30Z")

</div>

Finally i did it with your help, sorry if it is not the way to do, sure it can be done better, but it allow me to do what i need and that’s make me happy 🙂 Sometimes, the best is the ennemy of the good 😉

Here is the code:

```auto
$ComboBoxResult = $XmlLTI.countries.country | Where-Object CountryDisplayName -eq 'China'
$NodesList = $ComboBoxResult.psobject.BaseObject | Get-Member | Where-Object -Property MemberType -EQ -Value Property | Select-Object -Property Name
foreach ($Node in $NodesList) {
    Write-host "create new variable $($Node.Name) with value $($ComboBoxResult.$($Node.Name)) "  
    New-Variable -Name $($Node.Name) -Value $($ComboBoxResult.$($Node.Name)) -Force
    }

```

---

<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:** [October 28, 2022, 3:20pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/14 "2022-10-28T15:20:52Z")

</div>

OK. I’m glad it helped. 👍🏽

But now I’m curious. How do you use these variables? 🤔 You don’t know their names in advance. 🤷🏽‍♂️

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [October 31, 2022, 2:31pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/15 "2022-10-31T14:31:16Z")

</div>

Yes Olaf you are totally right, but i’m using these values lot of time in my script, and it’s really easier to create it all altough there is few unused . Thank you again.

---

<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:** [October 31, 2022, 3:01pm UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/16 "2022-10-31T15:01:16Z")

</div>

Hmmm … no - I am actually curious. Could you explain or show how you use these programmatically created variables later on?

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [November 3, 2022, 9:23am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/17 "2022-11-03T09:23:07Z")

</div>

example 1: exporting to another xml file :

```auto

[xml]$LTIXML = Get-Content -Path $LTIRESULTSXML
			$LTIXML.LTIRESULTS.country.CountryDisplayName = $CountryDisplayName
			$LTIXML.LTIRESULTS.country.CountryCode = $CountryCode
			$LTIXML.LTIRESULTS.JoinDomain.ComputerName = $tpcname.ToUpper() ...

```

example 2: displaying value in the gui

```auto
	#Set variables
	$labelSummaryValue1.text = $Summarylabel1Value
	$labelSummaryValue2.text = $CountryDisplayName
	$labelSummaryValue3.text = $combobox1.text.ToUpper()
	$labelSummaryValue4.text = $ADDomainNetbiosName
	
	#Set OU 
	$labelSummaryValue5.text = $ADOrgUnit #OU Cible

```

…

---

<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:** [November 3, 2022, 11:13am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/18 "2022-11-03T11:13:36Z")

</div>

hmmm … I’m afraid we do not talk about the same topic. I was curious how you use variables you created programmatically when you’re actually not know in advance what their names are.

---

<div class="post-metadata">

**Author:** ![GiTong](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gitong/32/4029_2.png) [@GiTong](https://forums.powershell.org/u/GiTong)\
**Post date:** [November 5, 2022, 9:34am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/19 "2022-11-05T09:34:21Z")

</div>

I’m not using those that i don t know in the main script 🙂 but it was quicker to use the loop to create all the variables based on the xml than specifying it all one by one.

---

<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:** [November 5, 2022, 9:49am UTC](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757/20 "2022-11-05T09:49:56Z")

</div>

> [@GiTong](#):
>
> create all the variables based on the xml

Hmmm … that’s actually what I meant. I’d consider this as unnecessary cumbersome complex. In this case you can simply use the XML variable reference … like this: `$XmlLTI.countries.country`🤷🏽‍♂️

But thanks anyway for explaining. 👍🏽

[Next page](https://forums.powershell.org/t/bulk-create-variables-from-xmllinkednode/20757.md?page=2)
