How do I pass in a string with spaces into PowerShell?

前端 未结 4 1629
庸人自扰
庸人自扰 2020-12-18 21:38

Given:

# test1.ps1
param(
    $x = \"\",
    $y = \"\"
)

&echo $x $y

Used like so:

powershell test.ps1
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 22:20

    Well, this is a cmd.exe problem, but there are some ways to solve it.

    1. Use single quotes

      powershell test.ps1 -x 'hello world' -y 'my friend'
      
    2. Use the -file argument

      powershell -file test.ps1 -x "hello world" -y "my friend"
      
    3. 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"
      

提交回复
热议问题