# Understanding ForEach loops and use variables

**URL:** <https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855>\
**Category:** PowerShell Help\
**Created:** [September 8, 2023, 2:24pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855 "2023-09-08T14:24:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![frogman7](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@frogman7](https://forums.powershell.org/u/frogman7)\
**Post date:** [September 8, 2023, 2:24pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/1 "2023-09-08T14:24:05Z")

</div>

I am new to PowerShell and need to understand why this does not work:

This first example works the way I want.

```auto
Function Get-FileName($initialDirectory){  
 [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = “All files (*.*)| *.*”
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

$Document = [xml]''
 Write-Host "Please select the Actual file."
 $ActualFile = Get-FileName -initialDirectory “$HOME\Downloads” # This function gets the file name of the file you want to create an object.

[xml]$Document = Get-Content $ActualFile

Foreach($TechRcrdId in $Document.BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId){
       Write-Host "TechRcrdId :" $TechRcrdId  
      }

This does not do what I want but I want to use a variable for the path.

$Document = [xml]''

 Write-Host "Please select the Actual file."
 $ActualFile = Get-FileName -initialDirectory “$HOME\Downloads” # This function gets the file name of the file you want to create an object.

 [xml]$Document = Get-Content $ActualFile
 $NodePath = "BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId"

Foreach($TechRcrdId in $Document.$NodePath){
       Write-Host "TechRcrdId :" $TechRcrdId  
      }

```

---

<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:** [September 8, 2023, 2:27pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/2 "2023-09-08T14:27:38Z")

</div>

Kenneth,  
Welcome to the forum. 👋🏼

Before we proceed … could you please go back, edit your question once again and fix the formatting of your code?  
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) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

---

<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:** [September 8, 2023, 2:54pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/3 "2023-09-08T14:54:05Z")

</div>

I googled for you … this might help:

> <https://stackoverflow.com/questions/72962308/powershell-variables-for-xml-node-path-addressing>

And BTW: The function `Get-FileName` is for your question completely irrelevant. You should have omit it. 😉

---

<div class="post-metadata">

**Author:** ![frogman7](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@frogman7](https://forums.powershell.org/u/frogman7)\
**Post date:** [September 8, 2023, 8:05pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/4 "2023-09-08T20:05:08Z")

</div>

That google search shows how to split out each node in the chain. I am looking for a way to use a variable to replace the dot path. Also I don’t really have namespaces in the XML.

---

<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:** [September 8, 2023, 8:23pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/5 "2023-09-08T20:23:07Z")

</div>

Hmm ok … how about this?

```auto
[xml]$Document = Get-Content -Path 'path to XML File.xml'

$TechRcrdIdList = $Document.BizData.Pyld.Document.SctiesFincgRptgTxRpt.TradData.Rpt.Err.TechRcrdId

Foreach ($TechRcrdId in $TechRcrdIdList ) {
    Write-Host "TechRcrdId :" $TechRcrdId  
}

```

Why do you actually want to do that with a variable?

---

<div class="post-metadata">

**Author:** ![frogman7](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@frogman7](https://forums.powershell.org/u/frogman7)\
**Post date:** [September 8, 2023, 8:37pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/6 "2023-09-08T20:37:57Z")

</div>

Just starting out with PowerShell so I am taking baby steps.  
What I want to do is better a compare tool built in PowerShell that does not order the repeatable field (could be 1 or Many) so the compare fails.  
It might be better for me to build my own compare tool that compares true XML as the current one just breaks up the XML the compares based on the trade strings. I am trying to find a way to order the XML so the strings will compare and not give false positives.  
My xml paths will be many as my documents can have hundreds of trades so I think making my Xpath a variable is the best way to go.

---

<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:** [September 8, 2023, 8:48pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/7 "2023-09-08T20:48:36Z")

</div>

OK. I am not an XML expert at all but how would it help you to have the path in a variable?

---

<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:14pm UTC](https://forums.powershell.org/t/understanding-foreach-loops-and-use-variables/22855/8 "2024-05-16T20:14:40Z")

</div>

Closed on May 16, 2024, 8:14pm UTC


