escaping single quote sign in PowerShell

sar*_*04x 3 powershell

I have a replace statement in my code whereby Band's is being replaced by Cig's. However when I put single quote it took the first sentence... Example 'Band'

I tried to use double quote but it does not work. Do you know how to escape the single quote sign?

-replace 'Band's', 'Cig's'
Run Code Online (Sandbox Code Playgroud)

use*_*740 7

See Escape characters, Delimiters and Quotes and Get-Help about_Quoting_Rules from the built-in help (as pointed out by as Nacimota).

To include a ' inside a single-quoted string, simply double it up as ''. (Single-quote literals don't support any of the other escape characters.)

> "Band's Toothpaste" -replace 'Band''s', 'Cig''s'
Run Code Online (Sandbox Code Playgroud)

Or, simply switch to double-quotes. (Double-quote literals are required when wishing to use interpolation or escape characters.)

> "Band's Toothpaste" -replace "Band's", "Cig's"
Run Code Online (Sandbox Code Playgroud)

(Don't forget that -replace uses a regular expression)

  • 不要忘记内置的帮助系统:[`Get-Help about_Quoting_Rules`](http://technet.microsoft.com/en-us/library/hh847740.aspx) (5认同)