ajeh
ajeh

Reputation: 2784

PowerShell script does not receive arguments no matter what

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

Upvotes: 0

Views: 1410

Answers (2)

hvyledmark
hvyledmark

Reputation: 21

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

Upvotes: 2

imahaare
imahaare

Reputation: 1

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!

Upvotes: 0

Related Questions