question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using hyphen/dash parameter produces "Cannot process argument because the value of argument “name” is not valid"

See original GitHub issue

Using 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Sep 12, 2019

@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:

C:\> pwsh -Command ./Test.ps1 -  # note the "./" - should work in both WinPS and PS Core

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.:

C:\> pwsh -Command "& \"./Test.ps1\" -"
0reactions
iSazonovcommented, Sep 16, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found