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

前端 未结 4 1631
庸人自扰
庸人自扰 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:21

    I had a similar problem but in my case I was trying to run a cmdlet, and the call was being made within a Cake script. In this case the single quotes and -file argument did not work:

    powershell Get-AuthenticodeSignature 'filename with spaces.dll'
    

    Resulting error: Get-AuthenticodeSignature : A positional parameter cannot be found that accepts argument 'with'.

    I wanted to avoid the batch file if possible.

    Solution

    What did work was to use a cmd wrapper with /S to unwrap outer quotes:

    cmd /S /C "powershell Get-AuthenticodeSignature 'filename with spaces.dll'"
    

提交回复
热议问题