Using -Args with -Command does not work as documented
See original GitHub issueThe powershell command line docs suggest that the “-Command” option may be used with the “-args” option to pass extra arguments to a script block. However, this does not work as described.
Recommend updating the command line documentation to document the actual behavior of -Command, which does not support an -args
option.
Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
Steps to reproduce
powershell.exe -Command "{ echo $args[1] }" -args one two three
Expected behavior
print two
Actual behavior
error:
aaronla@aaronla-r:scratch$ dotnet pwsh -command "{ echo $args[1] }" -args one two three
ParserError:
Line |
1 | { echo $args[1] } -args one two three
| ~~~~~
| Unexpected token '-args' in expression or statement.
Environment data
aaronla@aaronla-r:PowerApps-CoreServices$ dotnet pwsh -command echo $PSVersionTable
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:18
Top Results From Across the Web
Command does not escape arguments as expected on ...
Currently the windows implementation of Command will automatically attempt to escape arguments before concatenating them into a single command ...
Read more >How do I use the lines of a file as arguments of a command?
Documented in the bash man page in the 'Command Substitution' section. ... In my case using (Neo)Vim it does not and the args...
Read more >kubectl run --command vs -- arguments - kubernetes
If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you...
Read more >Define a Command and Arguments for a Container
This page shows how to define commands and arguments when you run a container in a Pod. Before you begin You need to...
Read more >Where is the `--` (double dash) argument documented?
The intent is to document all commands that do not follow the guidelines in their POSIX man page, from POSIX chapter 12.02 third...
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
Thanks for clarifying @rjmholt. The docs make it seem like it’s a command line argument, but now I see what you mean – it’s more intended as a powershell function argument to be used from within powershell, and not to be used at the command line / StartProcess / exec.
Given the discussion in the issue, I think the docs already cover this case and have marked it resolved. If there are changes you’d like to see in the documentation, please open an issue in https://github.com/MicrosoftDocs/PowerShell-Docs.