title: PowerShell: Pass local variable to function
body: <p>I have the following Powershell code:</p>
  
  <pre><code>function readConfigData
  {
      $workingDir = (Get-Location).Path
      $file = ""
  
      if ($Global:USE_LOCAL_SERVER)
      {
          $file = $workingDir + '\Configs\Localhost.ini'
      }
      else
      {
          $file = $workingDir + '\Configs\' + $env:COMPUTERNAME + '.ini'
      }
  
      Write-Host 'INIFILE: ' $file
  
      if (!$file -or ($file = ""))
      {
          throw [System.Exception] "Ini fil är inte satt."
      }
      if (!(Test-Path -Path $file))
      {
          throw [System.Exception] "Kan inte hitta ini fil."
      }
  }
  
  readConfigData
  </code></pre>
  
  <p>How should I declare the local variable <code>$file</code> that can be passed to the function <code>Test-Path</code>.
  My local variable $file get populated but then when I place it as argument to other function it's like it is out of scope.</p>
  
  <p>I read the <a href="https://technet.microsoft.com/en-us/library/hh847849.aspx" rel="nofollow">about scopes</a> article but wasn't able to figure it out.</p>
  
  <p>Currently I get the error:</p>
  
  <blockquote>
    <p>INIFILE:  D:\Projects\scripts\Configs\HBOX.ini Test-Path : Cannot bind argument to
    parameter 'Path' because it is an empty string. At
    D:\Projects\freelancer.com\nero2000\cmd script to
    powershell\script.ps1:141 char:27
    +     if (!(Test-Path -Path $file))
    +                           ~~~~~
        + CategoryInfo          : InvalidData: (:) [Test-Path], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand</p>
  </blockquote>