Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to escape a string in powershell like @"string" in C#

Tags:

powershell

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

like image 692
Jakob Avatar asked Apr 05 '15 17:04

Jakob


People also ask

How do you escape a string in PowerShell?

This means that if you hard code a Distinguished Name in PowerShell, and the string is enclosed in double quotes, any embedded double quotes must be escaped first by a backtick "`", and then by a backslash "\".

How do I ignore special characters in PowerShell?

PowerShell Regular Expressions Escape special characters To use these characters, as a . , + etc., in a pattern, you need to escape them to remove their special meaning. This is done by using the escape character which is a backslash \ in regex. Example: To search for + , you would use the pattern \+ .


3 Answers

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

like image 66
mjolinor Avatar answered Oct 20 '22 03:10

mjolinor


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
!
"
"@
like image 40
Tim Pietzcker Avatar answered Oct 20 '22 05:10

Tim Pietzcker


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

like image 2
Jakob Avatar answered Oct 20 '22 05:10

Jakob