title: powershell - loop through file directory of xml files and replace specific nodes for each file
body: <p>I am trying to loop through a specific file directory which holds only xml files, and replace specifically defined nodes in each of these files. My issue right now is the file path is being defined as c:\users\myusername\filepath rather than sticking to the original filepath defined in the script. This happens during my $xml assignment, as the $filepath variable is working properly for other commands in the script. Can I not use Get-Content here? I was under the impression that get-content should work since its editing 1 file at a time.</p>
  
  <p>Current Code:</p>
  
  <pre><code>#updating xml and not including sub-directories
             $filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
             Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){
  
              $xml = [xml](Get-Content $file)
  
              #updating attributes at each node
              $node = $xml.ConsensusModelingConfig #location of internal node tree
              $node.APPServer = "server1"
              $node.APPDB = "ConsensusModelingAPP"
              $node.SSASServer = "server1SSAS"
              $node.SSASDB = "ConsensusModelingDB"
              $xml.Save($filePath)
  
              }
  </code></pre>
  
  <p>Example of a file in the folder that I'm trying to edit/result I'm trying to create. These are currently different values and I want to set them to the values below. There are 4 of these. ConsensusModeling.xml:</p>
  
  <pre><code>&lt;ConsensusModelingConfig&gt;
    &lt;APPServer&gt;Server1&lt;/APPServer&gt;
    &lt;APPDB&gt;ConsensusModelingAPP&lt;/APPDB&gt;
    &lt;SSASServer&gt;Server1SSAS&lt;/SSASServer&gt;
    &lt;SSASDB&gt;ConsensusModelingDB&lt;/SSASDB&gt;
  &lt;/ConsensusModelingConfig&gt;
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>Get-Content : Cannot find path 'C:\Users\myusername\mytestfile
  At \\scriptlocation.ps1:628 c
  +                 $xml = [xml](Get-Content $file)
  +                              ~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: 
  (C:\Users\Alex.p...eling_Score.xml:String) [G
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
  </code></pre>
  
  <p>According to the error message, my path is getting assigned to C:\users etc instead of staying the original filepath defined in the $filepath variable. Why is this?</p>
  
  <p>Solution:
  As per @mklement0's comment, the following code fixed my issues and now all files are having their attributes properly updated.</p>
  
  <pre><code>#updating xml and not including sub-directories
             $filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
             Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){
  
              $xml = [xml](Get-Content $file.FullName)
  
              #updating attributes at each node
              $node = $xml.ConsensusModelingConfig #location of internal node tree
              $node.APPServer = "server1"
              $node.APPDB = "ConsensusModelingAPP"
              $node.SSASServer = "server1SSAS"
              $node.SSASDB = "ConsensusModelingDB"
              $xml.Save($file.FullName)
  
              }
  </code></pre>