Using hyphen/dash parameter produces "Cannot process argument because the value of argument “name” is not valid"
See original GitHub issueUsing the latest PowerShell 6.2.2:
C:\Test>pwsh -File Test.ps1 -
C:\Test\Test.ps1 : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again.
+ CategoryInfo : InvalidArgument: (:) [Test.ps1], PSArgumentException
+ FullyQualifiedErrorId : Argument,Test.ps1
The Test.ps1
is simple
echo "foo"
The error does not make any sense to me. I suspect it’s a bug in command-line parsing.
PowerShell 5.1 also processes the dash strangely (although it manifests differently): What does hyphen/dash parameter mean to PowerShell?
PowerShell 2.0 works as expected.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (1 by maintainers)
Top Results From Across the Web
What does hyphen/dash parameter mean to PowerShell?
C:\path\Test.ps1 : Cannot process argument because the value of argument "name" is not valid. Change the value o f the "name" argument and...
Read more >Can shell variable name include a hyphen or dash (-)?
I've never met a Bourne-style shell that allowed - in a variable name. Only ASCII letters (of either case), _ and digits are...
Read more >Part XII – Advanced Function Troubleshooting (OpenFaaS)
ps1 : Cannot process argument because the value of argument “name” is not valid. Change the value of the “name” argument and run...
Read more >about Command Syntax - PowerShell
The topic name is the value of the Name parameter. In a PowerShell command, parameter names always begin with a hyphen.
Read more >Arg in clap - Rust
Create a new Arg with a unique name. The name is used to check whether or not the argument was used at runtime,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@martinprikryl
The problem exists in Windows PowerShell and (no longer) in PowerShell Core as of the current preview (Core 7.0.0-preview.3). I do see it in 6.2.2, though (not sure what the policy re fixes is there).
I’m not sure how likely back-porting of fixes to Windows PowerShell is at this point, but you can also report the issue at Windows PowerShell UserVoice
As for a workaround: use
-Command
instead of-File
; while that changes the semantics of how the command line is parsed, in simple cases such as this one it should be fine:Note the
./
, because using-Command
(-c
) makes PowerShell parse the arguments as if they were PowerShell code, and the usual restrictions re executing scripts by filename only apply.If your script file path needed quoting, you’d have to use enclosing and nested quoting and prepend
&
; e.g.:I guess we haven’t direct commit for the issue, it was a side effect from adding new parameters. So it is not easy backport the change.