Given:
# test1.ps1 param( $x = "", $y = "" ) &echo $x $y
Used like so:
powershell test.ps1
Outputs:
> <blank line>
But then this goes wrong:
test.ps1 -x "Hello, World!" -y "my friend"
Outputs:
Hello, my
I was expecting to see:
Hello, World! my friend
Use Single Quotes ' ' to Deal With Spaces in the Path in PowerShell.
In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (""). Also try using the Grave Accent Character (`) PowerShell uses the grave accent (`) character as its escape character.
Spaces around special charactersWhite-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code.
Well, this is a cmd.exe
problem, but there are some ways to solve it.
Use single quotes
powershell test.ps1 -x 'hello world' -y 'my friend'
Use the -file
argument
powershell -file test.ps1 -x "hello world" -y "my friend"
Create a .bat
wrapper with the following content
@rem test.bat @powershell -file test.ps1 %1 %2 %3 %4
And then call it:
test.bat -x "hello world" -y "my friend"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With