Skip to content

Start-Process doesn't pass arguments with embedded whitespace and double quotes correctly #5576

@mklement0

Description

@mklement0

Updated later to include the problem with embedded double quotes.

Steps to reproduce

Embedded whitespace:

'"Hi!"' > './t 1.ps1'; Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile', '-file', './t 1.ps1'

Embedded double quotes:

Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile', '-command', '"Hi!"'

Expected behavior

In both cases:

Hi!

That is, script file ./t 1.ps1 should execute, and double-quoted string literal "Hi!" should print.

Actual behavior

Embedded whitespace:

Invocation fails, because the ./t 1.ps1 is passed as two arguments:

The argument './t' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.

The only way to make this currently work is to embed (potentially escaped) double quotes: '"./t 1.ps1"' or "`"./t 1.ps1`""; e.g.:

Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile', '-file', "`"./t 1.ps1`""

Update: Overall, the best workaround is to pass a single string containing all arguments to -ArgumentList and use embedded quoting and escaping as necessary:

Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile -file "./t 1.ps1"'

Embedded double quotes:

The embedded double quotes are unexpectedly removed.

The only way to make this currently work is to \-escape the embedded double quotes:

Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile', '-command', '\"Hi!\"'

Update: Again, the best workaround is to use a single string:

Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-noprofile -command \"Hi!\"'

Environment data

PowerShell Core v6.0.0-rc (v6.0.0-rc) on Microsoft Windows 7 Enterprise  (64-bit; v6.1.7601)
Windows PowerShell v5.1.14409.1012 on Microsoft Windows 7 Enterprise  (64-bit; v6.1.7601)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions