Why can't we have a -Path C:/some/path
inside a string parameter?
Using -Command:
pwsh.exe -Command .\hello.ps1 -A 123 -B "-Path a/b/c"
hello.ps1: A positional parameter cannot be found that accepts argument 'a/b/c'.
pwsh.exe -Command .\hello.ps1 -A 123 -B '-Path a/b/c'
hello.ps1: A positional parameter cannot be found that accepts argument 'a/b/c'.
Using -File:
pwsh.exe -File .\hello.ps1 -A 123 -B "-Path a/b/c"
Hello!
B: -Path a/b/c
Good! Now with a rooted path:
pwsh.exe -File .\hello.ps1 -A 123 -B "-Path C:/a/b/c"
hello.ps1: Missing an argument for parameter 'B'. Specify a parameter of type 'System.String' and try again.
Hmm.. now with a unix style path:
pwsh.exe -File .\hello.ps1 -A 123 -B "-Path /c/a/b/c"
Hello!
B: -Path /c/a/b/c
ok... but what's wrong with the colon?
pwsh.exe -File .\hello.ps1 -A 123 -B "-Path :"
hello.ps1: Missing an argument for parameter 'B'. Specify a parameter of type 'System.String' and try again.
pwsh.exe -File .\hello.ps1 -A 123 -B ":"
Hello!
B: :
So it's really when the string has -Something
before the colon that the issue arise. Is there a way to escape that colon?
(PSVersion 7.2.0)
[EDIT] hello.ps1 would be a run script that spawns a subprocess (could be any executable, including pwsh.exe)
[EDIT (based on answers)]
Possible wordarounds:
- Quotes:
pwsh.exe -Command "./hello.ps1 -A 123 -B '-Path C:/a/b/c'"
- Script block and quotes:
pwsh.exe -Command "& {./hello.ps1 -A 123 -B '-Path C:/a/b/c'}"