You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using -Args with -Command does not work as documented #15410
The 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.
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
It works -- but it only works if the shell you're running it in is itself powershell/pwsh. The scriptblock handling that allows -Command to take an actual scriptblock requires that the shell handing the argument to the executable is powershell or pwsh, which will actively serialize the input to a format that the new shell instance can parse.
Without that, everything following -Command is assumed to be a single command string/script, so -Args is never read as an executable argument, but interpreted as part of the command string.
If that's the intended behavior, that this feature only work from specific shells, then perhaps that's the scope of the bug. Update the documentation to reflect that this only works on bash and powershell, and not via exec / CreateProcess / batch / Windows command line.
The Command parameter only accepts a script block for execution when it can recognize the value passed to Command as a ScriptBlock type. This is only possible when running powershell.exe from another PowerShell host. The ScriptBlock type may be contained in an existing variable, returned from an expression, or parsed by the PowerShell host as a literal script block enclosed in curly braces ({}), before being passed to powershell.exe.
using the powershell function/alias
It's working as the standard executable here, it's just that the invoking PowerShell process is able to translate the scriptblock passed as an argument into an encoded command under the hood.
Activity
jhoneill commentedon May 14, 2021
The command needs to be written as a script block, not a string, and the args as an array
pwsh -command { echo $args[1] } -args one,two,threeWhen a string is used in place of the script block,
-argsisn't allowed ,but you can do something likepwsh -c "dir" "function:"vexx32 commentedon May 15, 2021
It works -- but it only works if the shell you're running it in is itself powershell/pwsh. The scriptblock handling that allows
-Commandto take an actual scriptblock requires that the shell handing the argument to the executable is powershell or pwsh, which will actively serialize the input to a format that the new shell instance can parse.Without that, everything following
-Commandis assumed to be a single command string/script, so-Argsis never read as an executable argument, but interpreted as part of the command string.237dmitry commentedon May 15, 2021
It works in bash:
returned:
In my opinion the better way is to start executable (
chmod +x) pwsh-scripts with shebang#!/usr/bin/pwsh -NoProfilejhoneill commentedon May 15, 2021
My brain is having trouble with writing a batch file that runs PowerShell (why not have everything in one PS script) but this works
powershell -noprofile -command "echo %*"rjmholt commentedon May 17, 2021
Sean Wheeler (@sdwheeler) are you able to transfer this to PowerShell-Docs?
237dmitry commentedon May 17, 2021
It works, I think string arguments in
.Invoke()should be in quotation marks:rjmholt commentedon May 17, 2021
From about_powershell.exe:
It's working as the standard executable here, it's just that the invoking PowerShell process is able to translate the scriptblock passed as an argument into an encoded command under the hood.
rjmholt commentedon May 17, 2021
Also please note that the repo for that is https://github.com/PowerShell/PowerShell-Docs.
sdwheeler commentedon May 17, 2021
This works, as stated in the docs, when it is run from within a PowerShell session.
rjmholt commentedon May 17, 2021
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.