PowerShell: concatenate strings with variables after cmdlet

后端 未结 1 1133
一整个雨季
一整个雨季 2021-02-20 05:25

I often find myself in the situation where I have to concatenate a string with a variable after a cmdlet. For example,

New-Item $archive_path + "logfile.txt&         


        
1条回答
  •  情歌与酒
    2021-02-20 05:56

    You get that error because the PowerShell parser sees $archive_path, +, and "logfile.txt" as three separate parameter arguments, instead of as one string.

    Enclose the string concatenation in parentheses, (), to change the order of evaluation:

    New-Item ($archive_path + "logfile.txt") -Type file
    

    Or enclose the variable in a subexpression:

    New-Item "$($archive_path)logfile.txt" -Type file
    

    You can read about argument mode parsing with Get-Help about_Parsing.

    0 讨论(0)
提交回复
热议问题