title: Passing quoted arguments to PowerShell scripts
body: <p>Scripting is made for CLI and, in the Microsoft world, script arguments referring to files may often have spaces requiring them to be quoted.</p>
  
  <p>This turns out to be problematic for me. Consider this script:</p>
  
  <pre><code>#testarg.ps1 
  for($i=0; $i -lt $args.length; $i++)
  {
      write-host "Arg$i = $($args[$i])"
  } 
  </code></pre>
  
  <p>If I run it inside the PowerShell interactive environment, like this:</p>
  
  <pre><code>PS C:\script&gt; .\testarg.ps1 "first arg"  "second arg"
  </code></pre>
  
  <p>I get, as expected,</p>
  
  <pre><code>Arg0 = first arg
  Arg1 = second arg
  </code></pre>
  
  <p>But the ordinary use of a script would be, outside the interactive mode, in a batch one. As far as I understand, <a href="http://technet.microsoft.com/en-us/scriptcenter/374e2db5-89cf-4217-85e6-ca0014271059" rel="nofollow">the Microsoft suggested way</a> to run scripts from <code>cmd.exe</code> seems to be <code>powershell.exe path\to\script.ps1</code> (without space in path). But:</p>
  
  <pre><code>powershell.exe .\testarg.ps1 "first arg"  "second arg"
  </code></pre>
  
  <p>gives the inconsistent result:</p>
  
  <pre><code>Arg0 = first
  Arg1 = arg
  Arg2 = second
  Arg3 = arg 
  </code></pre>
  
  <p>I noted that to obtain the intended result I can use single quotes: </p>
  
  <pre><code>powershell.exe .\testarg.ps1 'first arg' 'second arg'
  </code></pre>
  
  <p>But this is often not viable when the script is to be run by GUI tools that automatically generate and pass the arguments, and/or in presence of more CLI tools for reason of consistency in the use of the quotes.</p>
  
  <p>For some reason when using the <code>-file</code> option:</p>
  
  <pre><code>powershell.exe –file .\testarg.ps1 "first arg"  "second arg"
  ::(reference to relative path '.\' is optional)
  </code></pre>
  
  <p>I get again the intended result: </p>
  
  <pre><code>Arg0 = first arg
  Arg1 = second arg
  </code></pre>
  
  <p>So with the <code>-file</code> option the double quotes work properly. This suggests that one can remap the Windows file type association for <code>ps1</code> files (with <code>FTYPE.exe</code>) to something like:</p>
  
  <pre><code>FTYPE Microsoft.PowerShellScript.1=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "%1" %*
  </code></pre>
  
  <p>Given the FTYPE association, I can obtain the intended result by simply writing:</p>
  
  <pre><code>testarg.ps1 "first arg"  "second arg"
  </code></pre>
  
  <p>The latter is quite satisfying for me, but since I am newbie with PowerShell, I wonder: </p>
  
  <ol>
  <li>For a general script, are the batch form <code>powershell.exe –file path\to\script.ps1</code> and the interactive form (from within PowerShell) <code>PS &gt; path\to\script.ps1</code> equivalent? Are there some instances when the two forms may produce different output/effects?</li>
  <li>Is there a possible solution inside the script to read the command line as is (without stripping double quotes)? Something like <code>*%</code> for <code>cmd.exe</code>.</li>
  </ol>