title: PowerShell Function to return a variable
body: <p>I'm writing a function to create a variable called <code>$Credentials</code> which I use in another function to verify if it's valid or not. By using <code>return $Credentials</code>in the function I hoped the variable would've been available after the <code>Function Import-Password</code> has ran, but this isn't the case.. </p>
  
  <p>I think this could be solved by using <code>$script:Credentials</code>, but I don't think it's needed if the function can output this variable.</p>
  
  <p>Thank you for your help.</p>
  
  <p><strong>Function Import-Password</strong></p>
  
  <pre><code># Check the password file
  Function Import-Password ($UserName,$PasswordFile,[switch]$SendMail) {
      try {
          if (!(Test-Path $PasswordFile)) {
              Write-Host "$env:COMPUTERNAME &gt; Check password file: $PasswordFile &gt; The password file can not be found`n
              - Password file :`t $PasswordFile
              - Server name   :`t $env:COMPUTERNAME" -ForegroundColor Yellow
              if ($SendMail) {
                  Send-Mail "FAILED AD Authentication" "The password file can not be found" "- Password file : $PasswordFile&lt;br&gt;- Server name   : $env:COMPUTERNAME"
              }
              break
          }
          $Password = cat $PasswordFile | ConvertTo-SecureString -Force -ErrorAction Stop
          $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName,$Password 
          return $Credentials
      }
      catch {
          Write-Host "$env:COMPUTERNAME &gt; Check password file: $PasswordFile &gt; The password has been hashed with another account than the account used to run this script (all 3 users/owners need to be the same)`n
          - Script account:`t $env:USERDNSDOMAIN\$env:USERNAME
          - Password user :`t $UserName
          - Password file :`t $PasswordFile
          " -ForegroundColor Yellow
          if ($SendMail) {
              Send-Mail "FAILED AD Authentication" "The password has been hashed with another account than the account used to run this script (all 3 users/owners need to be the same)" "- Script account: $env:USERDNSDOMAIN\$env:USERNAME&lt;br&gt;- Password user : $UserName&lt;br&gt;- Password file : $PasswordFile"
          }
          break
      } 
  }
  Import-Password $UserName $PasswordFile
  </code></pre>