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.

Passing arguments with the Start-Process cmdlet

See original GitHub issue

Prerequisites

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:closed
  • Created 8 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Jan 8, 2023

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.

    • Applied to your case, using a single string:
# Note: With #5576 fixed as proposed, -ArgumentString could be used in lieu of -ArgumentList
Start-Process -FilePath 'pwsh' -ArgumentList '-Command Write-Host "`\"Hello World!`\""' -Wait -NoNewWindow

1reaction
mklement0commented, Jan 8, 2023

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.

Read more comments on GitHub >

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

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