title: Powershell line of code does not run when called through function but will run directly. What could be possible causes?
body: <p>I have a powershell script with the following code in it...</p>
  
  <pre><code>$appdir = Split-Path -Path $MyInvocation.MyCommand.Path
  $xfrdir = $appdir + "\xfr\"
  $cfgFile = "ofx_config.cfg"
  $cfgPath = $appdir + "\" + $cfgFile
  $configData = New-Object System.Collections.ArrayList
  #  --- some other code here...
  function Load-Config () 
  {
      if (test-path ($cfgPath))
      {
          $configData = Import-Clixml -path "$cfgPath"
      } 
  }
  #  ---some other code here
  load-config
  </code></pre>
  
  <p>When I just RUN this script in the ps ISE, load-config runs because it is at the end of script (i verified this with a breakpoint) but the $configData variable remains empty.    </p>
  
  <p>But if I immediately copy and past the line <code>$configData = Import-Clixml -path "$cfgPath"</code> into the powershell command line and run it then $configData is loaded with data.  Does anyone have any ideas what might be going on? </p>
  
  <p><strong>EDIT</strong><br>
  I think that what you are saying is that $configData in <code>$configData = Import-Clixml -path "$cfgPath"</code> is being treated as a whole separate variable (and is local to the function) because of scoping rules.  I thought it would be more like a c# class and so would assign to the script level variable of the same name.   </p>
  
  <p>I am LOVING powershell but dynamic typing does make things trickier.</p>
  
  <p><strong>EDIT 2</strong><br>
  Both answers were insightful.  In such a case I usual give the answer to the person with the fewest reputation.  And I did in fact Andy's second example in any case.</p>
  
  <p>Seth</p>