Passing arguments with the Start-Process cmdlet
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I am experiencing issues running external programs with the Start-Process
cmdlet.
For example I think the following two commands should produce the same output but they do not:
PS> pwsh -Command 'Write-Host "`"Hello World!`""'
"Hello World!"
PS> Start-Process -FilePath 'pwsh' -ArgumentList @('-Command', 'Write-Host "`"Hello World!`""') -Wait -NoNewWindow
Hello World!`
There seems to be an issue with how parameters in the ArgumentList
are passed to the external program.
Expected behavior
PS> Start-Process -FilePath 'pwsh' -ArgumentList @('-Command', 'Write-Host "`"Hello World!`""') -Wait -NoNewWindow
"Hello World!"
Actual behavior
PS> Start-Process -FilePath 'pwsh' -ArgumentList @('-Command', 'Write-Host "`"Hello World!`""') -Wait -NoNewWindow
Hello World!`
Error details
No response
Environment data
PS> $PSVersionTable
Name Value
---- -----
PSVersion 7.3.1
PSEdition Core
GitCommitId 7.3.1
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created 8 months ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Start-Process (Microsoft.PowerShell.Management)
Specifies parameters or parameter values to use when this cmdlet starts the process. Arguments can be accepted as a single string with the...
Read more >pass arguments to the Start-Process script block
All pass-through arguments passed to Start-Process must be the elements of a single array passed to -ArgumentList .
Read more >Using Start-Process with -ArgumentList
My problem is that I get an error powershell.exe : Start-Process : A positional parameter cannot be found that accepts argument 'INSTALLDIR=D:\ ...
Read more >Passing variables to nested Start-Process : r/PowerShell
I've got a script that can take a parameter on launch, but it requires running as admin. So I have it nested behind...
Read more >PowerShell Start-Process with Arguments
-Wait Indicates that this cmdlet waits for the specified process and its descendants to complete before accepting more input. This parameter ...
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
Glad to hear it helped, @lclutz.
Yes, I think it’s fine to close it, and perhaps someone from the team, perhaps @iSazonov, can tag it as a duplicate.
Your issue did highlight an aspect that #5576 doesn’t explicitly mention yet, however:
When the original problem gets fixed, hopefully by introducing a new
-ArgumentArray
(-arga
) parameter, as proposed here, that parameter should perform the same automatic escaping that direct invocation does in 7.3.+ - that is, not only do values with spaces need to be enclosed in"..."
on the process command line, embedded"
must also be automatically escaped as\"
By contrast, the old behavior for
-ArgumentList
(-args
) must remain in effect for backward compatibility (with an alias of-ArgumentString
), which is then best used with a single-ArgumentList
value encoding all arguments, which means the user must construct the raw process command line themselves, i.e. with explicit embedded"
chars. and\
-escaping.Yes, unlike the Windows PowerShell CLI,
powershell.exe
, the PowerShell Core CLI,pwsh.exe
, now also supports""
as an escaped"
on the command line, as an alternative to\"
(the latter is usually preferable, because it is more universally recognized, and it is also what PowerShell itself uses when performing the escaping automatically in direct calls).The point is the same, however: with
Start-Process
, you must perform the escaping yourself.