PowerShell script does not receive arguments no matter what
<p>Trying to pass a single command line argument to a powershell script on Windows 7, but the script does not seem to recognize any arguments. It blasts through the first lines below</p>

<pre><code>foreach($arg in $args)
{
    Write-Host "Arg: $arg";
}
</code></pre>

<p>without outputting anything that I use on the command line and fails due to <code>$args[0]</code> being empty. However the rest of my script works if I instead hardcode the value of the variable I am trying to assign from the command line (it simply opens that file and does something).</p>

<p>I was inspired by this answer <a href="https://stackoverflow.com/questions/16426688/passing-a-variable-to-a-powershell-script-via-command-line">Passing a variable to a powershell script via command line</a> specifically by the link in the accepted answer, and tried using <code>param</code> block but that did not print out anything as well</p>

<pre><code>param(
    [string]$fileName
)
Write-Host "Filename: [ $fileName ]";
</code></pre>

<p>when invoked like <code>script.ps1 -filename SampleFile.txt</code></p>

<p>When I simply copy/paste the first script from the link into a new script:</p>

<pre><code>Write-Host "Num Args:" $args.Length;
foreach ($arg in $args)
{
  Write-Host "Arg: $arg";
}
</code></pre>

<p>and call it as <code>1.ps1 1 2 3</code> its output is only <code>Num Args: 0</code>.</p>

<p>What am I doing wrong?</p>

<p>PS: If it matters, here is version information</p>

<pre><code>PS Z:\&gt; $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
</code></pre>