Make PowerShell ignore semicolon

后端 未结 6 2406
清歌不尽
清歌不尽 2021-02-19 06:54

I want to execute a cmd on PowerShell and this command uses semicolons. Then PowerShell interprets it as multiple commands. How do I make PowerShell ignore the semicolons and ex

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 06:59

    The easiest way to ignore a semicolon? Simply use a single quote versus double quote!

    In PowerShell, the type of quote you use matters. A double quote will let PowerShell do string expansion (so if you have a variable $something = someprogram.exe, and run "$something", PowerShell substitutes in "someprogram.exe").

    If you don't need string substitution/variable expansion then just use single-quotes. PowerShell will execute single-quoted strings exactly as listed.

    Another option if you want to use string expansion is to use a here-string instead. A here string is just like a regular string, however it begins and ends with an @ sign on its own separate line, like so:

    $herestring = @"
    Do some stuff here, even use a semicolon ;
    "@
    

    This is a best-of-both-worlds scenario, as you can use your fancy characters and have them work, but still get Variable expansion, which you do not get with single-quotes.

提交回复
热议问题