28

Is there a way to escape a complete string i powershell the same way the @"string" Works in C# I am writing a script and in there I have several strings that looks like this: D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)

escaping one by one There's is a long way home

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
Jakob
  • 360
  • 1
  • 3
  • 10
  • Why are you needing to use @ on that string, you are not using `\ ` inside it so there is no need to do it. Also, as a FYI the official name for a `@"string"` is a "verbatim string literal" – Scott Chamberlain Apr 05 '15 at 17:08
  • Why is this question has -1 vote. It's a valid question. – chenz Mar 17 '16 at 03:14

3 Answers3

19

To quote verbatim strings in Powershell, just use single quotes:

'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)'

see

Get-Help about_Quoting_Rules

or Here

mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • This helped me run `npm test -- -w --grep='"/some.*(this|that)/i"'` to get all tests with 'some' (typically in the suite name) and containing 'this' or 'that' (typically in the test name). (the actual words have been changed). Thanks a bunch. OP should definitely mark this as accepted. – RoboticRenaissance Jan 19 '21 at 17:56
5

You don't need a verbatim string modifier in PowerShell since backslashes are literals (and quotes are escaped by doubling).

For a multiline string, use

$x = @"
"
Curiouser 
and 
curiouser
!
"
"@
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

The keyword I was missing was verbatim:
http://www.johndcook.com/blog/2008/01/22/c-verbatim-strings-vs-powershell-here-strings/
https://technet.microsoft.com/en-us/library/ee692792.aspx

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Jakob
  • 360
  • 1
  • 3
  • 10