Scott Lin
Scott Lin

Reputation: 1562

How to properly quote a PowerShell command path when used from CMD

Baseline command that works properly:

The following command is being executed in CMD prompt and runs successfully.

"C:\Program Files\PowerShell\6.0.4\pwsh.exe" -NoLogo -NoProfile -NonInteractive -Command "&{D:\FooBar\myscript.ps1 -DependentAssembliesDirectoryPath 'D:\FooBar' -OutputPath 'D:\Baz Qux\output' -DocumentVersion 'whatever' -VisualStudioXmlDocumentationPaths 'D:\Baz Qux\input\my.xml' -AssemblyPaths  'D:\Baz Qux\input\my.exe','D:\Baz Qux\input\my1.dll','D:\Baz Qux\input\my2.dll','D:\Baz Qux\input\my3.dll' -MajorOpenApiSpecificationVersion 3 -MinorOpenApiSpecificationVersion 0 -Format YAML -DocumentDescriptionFilePath 'D:\Baz Qux\input\my.md'}; EXIT $LASTEXITCODE"

However, when a space is introduced to the path of myscript.ps1, the command no longer works. This is expected since I need to properly quote the path. I cannot figure out the proper way of quoting though.

Invalid attempt at quoting the command:

I thought this would have worked based on the technique of quoting other paths in my command, but this doesn't work.

"C:\Program Files\PowerShell\6.0.4\pwsh.exe" -NoLogo -NoProfile -NonInteractive -Command "&{'D:\Foo Bar\myscript.ps1' -DependentAssembliesDirectoryPath 'D:\Foo Bar' -OutputPath 'D:\Baz Qux\output' -DocumentVersion 'whatever' -VisualStudioXmlDocumentationPaths 'D:\Baz Qux\input\my.xml' -AssemblyPaths  'D:\Baz Qux\input\my.exe','D:\Baz Qux\input\my1.dll','D:\Baz Qux\input\my2.dll','D:\Baz Qux\input\my3.dll' -MajorOpenApiSpecificationVersion 3 -MinorOpenApiSpecificationVersion 0 -Format YAML -DocumentDescriptionFilePath 'D:\Baz Qux\input\my.md'}; EXIT $LASTEXITCODE"

This command results in a bunch of errors like,

At line:1 char:119
+ ... ionsDocumentGeneration.ps1' -DependentAssembliesDirectoryPath 'D:\Ope ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '-DependentAssembliesDirectoryPath' in expression or statement.

At line:1 char:153
+ ... rectoryPath 'D:\Foo Bar' -OutputPath ...
+                 ~~~~~~~~~~~~
Unexpected token ''D:\Foo Bar'' in expression or statement.

Again, since it can be a little hard to see the difference, the delta from the baseline command that works and the second command is that &{D:\FooBar\myscript.ps1 changed to &{'D:\Foo Bar\myscript.ps1' to introduce a space in the path and attempt quoting.

Please Note

I cannot invoke the command in PowerShell because it is out of my control. It must be invoked in cmd.exe prompt.

Upvotes: 2

Views: 1417

Answers (1)

mklement0
mklement0

Reputation: 437062

The problem is that you need to use & in PowerShell in order to invoke a command name / executable file path that is quoted and/or specified via a variable:

Therefore, replace:

"&{'D:\Foo Bar\myscript.ps1' ... }; exit $LASTEXITCODE"

with:

"& { & 'D:\Foo Bar\myscript.ps1' ... }; exit $LASTEXITCODE"

That said, there's no reason to wrap the invocation of your *.ps1 script in a script-block invocation (& { ... }), so you can simplify your command to:

"& 'D:\Foo Bar\myscript.ps1' ...; exit $LASTEXITCODE"

Upvotes: 4

Related Questions