0

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

foreach($arg in $args)
{
    Write-Host "Arg: $arg";
}

without outputting anything that I use on the command line and fails due to $args[0] 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).

I was inspired by this answer Passing a variable to a powershell script via command line specifically by the link in the accepted answer, and tried using param block but that did not print out anything as well

param(
    [string]$fileName
)
Write-Host "Filename: [ $fileName ]";

when invoked like script.ps1 -filename SampleFile.txt

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

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

and call it as 1.ps1 1 2 3 its output is only Num Args: 0.

What am I doing wrong?

PS: If it matters, here is version information

PS Z:\> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
Community
  • 1
  • 1
ajeh
  • 2,652
  • 2
  • 34
  • 65
  • Can you provide [mcve]? I can not reproduce described behavior with your code. – user4003407 Jun 20 '16 at 20:50
  • I have provided the minimal, complete example, and I fully understand that this is supposed to work. However it does not work for me and the purpose of this question is to figure out why. Unfortunately as this does not work for me, I have no way of satisfying your requirement of the example to be verifiable. – ajeh Jun 20 '16 at 21:03

2 Answers2

2

I don't think it has anything to do with file linking, or registry hacking! :) When running the example code, I also get no return when using your code. But when you make the parameter MANDATORY, it starts to display the arguments. Just need to format the PARAM Correctly, as in:

  Param( [Parameter(Mandatory=$True)]
            [string]$argument_one
        ) 
  Write-Host "Hello World $argument_one"

There you have it. Now you can call it from CMD as in:

   powershell.exe -command "& 'thePSFile.ps1' -$argument_one 'it works now'"

I hope this Helps. I know, resurrected from the dead, but I thought some other people searching could see how they were almost there.

//ark

0

I have this problem as well. It took a while to recover what I have done previously.

First approach: make sure assoc, ftype and %PATHEXT% are set for powershell:

C:\>assoc .ps1
.ps1=Microsoft.PowerShellScript.1

C:\>ftype Microsoft.PowerShellScript.1
Microsoft.PowerShellScript.1="C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -noexit -file %1 %~2

C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw;.ps1

But this likely will not work in windows 7 or higher.

Then edit the registry (all cautions apply here)

Edit the registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\0\Command

Set it from

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" 

To

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" %~2

Good luck!

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Is all that supposed to matter when a PS1 script executes under PS command prompt? I would expect that to matter when executed from a regular command prompt (not that I tried that)... – ajeh Oct 28 '16 at 17:32