title: How to run a python program from powershell based on python hashbang?
body: <p>I have several python3 scripts using the <em>hashbang</em> for identifying it as a Python3 script. The scripts can <em>not</em> be identified as python based on the file extension, as they don't have any (just like in any other *nix system). </p>
  
  <p>I've already looked at related issues, but they don't address this particular need, and seem to rely upon the <code>*.py</code> extension, for recognition.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11472843/set-up-python-on-windows-to-not-type-python-in-cmd">Set up Python on Windows to not type &quot;python&quot; in cmd</a></li>
  <li><a href="https://stackoverflow.com/questions/4235834/how-to-make-python-scripts-executable-on-windows">How to make python scripts executable on Windows?</a></li>
  </ul>
  
  <p>So my script is named: <strong><code>myscript</code></strong>, and the first line of the file is:</p>
  
  <pre><code>#!/usr/bin/env python3
  ...
  </code></pre>
  
  <p><strong>How can I make Windows <em>powershell</em> recognize this and run it with the python interpreter located in <code>C:\Python3.7</code>?</strong>  </p>
  
  <hr>
  
  <p><strong>UPDATE-1</strong></p>
  
  <p>To clarify, I want to run it from <em>powershell</em> CLI and not by clicking on it.
  In addition, I just discovered (to my horror) that when you use <code>pip install</code> with a native Windows Python3, the first line <em>hashbang</em> is autotragically replaced with:  </p>
  
  <p><strong><code>#!c:\python37\python.exe</code></strong></p>
  
  <p>Ouch!</p>
  
  <hr>
  
  <p><strong>UPDATE-2</strong></p>
  
  <p>Thanks to @eryksun's comments, I managed to get a PowerShell script to do some basic checking for me. However, it will need to be fixed to support more than Python. </p>
  
  <p><strong><code>test4script.ps1</code></strong>:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>
  Param([parameter(Mandatory=$true, HelpMessage="Need a valid filename")] $fileName)
  $firstLine = Get-Content -Path $fileName -TotalCount 1
  $SHEBANG="^#!"
  $shes=@("python3","python2","python","bash","sh","perl","pwsh")
  
  If ($firstLine -match $SHEBANG) {
      Write-Host "DEBUG: checking for common shebangs" -ForegroundColor Green
      foreach ($i in $shes) {
          If ($firstLine -match $i) {
              Write-Host "DEBUG: found shebang for: $i" -ForegroundColor Green
              C:\python37\python.exe $fileName
              break
          }
      }
  } else {
      Write-Host "File is not a known script. No shebang fund in file!" -ForegroundColor Red
      return
  }
  Write-Host "DEBUG: Done" -ForegroundColor Green
  </code></pre>
  
  <p>The result is:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ Get-Content -Path nonscript -TotalCount 3
  #This aint right
  echo "hello"
  
  $ Get-Content -Path pip-describe -TotalCount 3
  #!c:\python37\python.exe
  # pip-describe - Show full text package description from PyPI
  # -*- coding: utf-8 -*-
  
  $ .\test4script.ps1 nonscript
  File is not a known script. No shebang fund in file!
  
  $ .\test4script.ps1 pip-describe
  DEBUG: checking for common shebangs
  DEBUG: found shebang for: python3
  
   Usage:  pip-describe &lt;package-name&gt;
  
   This will return the full-text package description (usually the README)
   as found on PyPI, for any given &lt;package-name&gt;.
   ...
  
  </code></pre>
  
  <p>Now we should be able to associate this script with <strong><code>.</code></strong>, using <strong>CMD.exe</strong> with:</p>
  
  <pre><code>cmd /c assoc .=unknown
  cmd /c "ftype unknown=""C:\mybin\test4script.ps1"" %1"
  </code></pre>
  
  <p>But it would have been nicer to do this independently with PowerShell and not having to hop through CMD.</p>