title: How can I use a shebang in a PowerShell script?
body: <p>I have several PowerShell scripts that I'd like to invoke directly as a command from a Bash shell in Cygwin. For example, if I write a script with the filename <i>Write-Foo.ps1</i>, I'd like to execute it as a command from any working directory:</p>
  
  <pre><code>$ Write-Foo.ps1 arg1 arg2 ...
  </code></pre>
  
  <p>To do this, I add the script to my PATH, make it executable, and include the following interpreter shebang/hashbang at the beginning of the file:</p>
  
  <pre><code>#!/usr/bin/env powershell
  
  Write-Host 'Foo'
  ...
  </code></pre>
  
  <p>It's a common (ab)use of the <i>env</i> utility, but it decouples the scripts from Cygwin's path prefix (<i>/cygdrive/c/...</i>), at least for the interpreter declaration.</p>
  
  <p>This works to start PowerShell, but the system passes the file as a Cygwin-formatted path, which PowerShell doesn't understand, of course:</p>
  
  <blockquote>
    <p>The term '/cygdrive/c/path/to/Write-Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.</p>
  </blockquote>
  
  <p>MSYS (Git Bash) seems to translate the script path correctly, and the script executes as expected, as long as the path to the file contains no spaces. Is there a way to invoke a PowerShell script directly by relying on the shebang in Cygwin?</p>
  
  <p>Ideally, I'd also like to omit the <i>.ps1</i> extension from the script names if possible, but I understand that I may need to live with this limitation. I want to avoid manually aliasing or wrapping the scripts if possible.</p>
  
  <hr>
  
  <p><a href="https://stackoverflow.com/questions/48216173/how-can-i-use-a-shebang-in-a-powershell-script#48218913"><strong>A quick note for Linux/macOS users finding this.</strong></a></p>