title: How powershell handles returns from & calls
body: <p>We have field devices that we decided to use a powershell script to help us handle 'updates' in the future. It runs every 5 minutes to execute <code>rsync</code> to see if it should download any new files. The script, if it sees any file types of .ps1, .exe, .bat ,etc. will then attempt to execute those files using the <code>&amp;</code> operator. At the conclusion of execution, the script will write the file executed an excludes file (so that <code>rsync</code> will not download again) and remove the file. My problem is that the return from the executed code (called by <code>&amp;</code>) behaves differently, depending on how the main script is called.</p>
  
  <p>This is the main 'guts' of the script:</p>
  
  <pre><code>Get-ChildItem -Path $ScriptDir\Installs\* -Include @("*.ps1","*.exe","*.cmd","*.VBS","*.MSI") | ForEach {
  
     Write-Verbose "Executing: $_"
     &amp; $_
     $CommandName = Split-Path -Leaf $_
     Write-Verbose "Adding $CommandName to rsync excludes"
     Write-Output "$CommandName" | Out-File -FilePath $ScriptDir\excludes     -Append -Encoding ASCII
     Write-Verbose "Deleting '$_'"
     Remove-Item $_
  }
  </code></pre>
  
  <p>When invoking powershell (<code>powershell.exe -ExecutionPolicy bypass</code>) and then executing the script (<code>.\Update.ps1 -Verbose</code>), the script runs perfectly (<em>i.e.</em> the file is written to excludes and deleted) and you can see the verbose output (writing and deleting).</p>
  
  <p>If you run the following (similar to task scheduler) <code>powershell.exe -ExecutionPolicy bypass -NoProfile -File "C:\Update.ps1" -Verbose</code>, you can see the new script get executed but none of the steps afterwards will execute (<em>i.e.</em> no adding to excludes or removing the file or the verbose outputs).</p>