Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell escape character ` (backtick) not escaping string

I'm having trouble adding a new line into a string in powershell:

Get-ChildItem "C:\Users\matt\Desktop\CShell Install" |foreach {'<Component Id="'+$_.name+'" Guid="' +[guid]::NewGuid() + '">`r`n<File Id="'+$_.name+'" Source="$(var.CShell.TargetPath)"></File></Component>'}

As you can see I want a newline to occur at

``r`n

instead they are printed literally.

Any pointers?

like image 899
m.edmondson Avatar asked Sep 11 '26 09:09

m.edmondson


1 Answers

Do not use Single Quote, where you want PowerShell to honor backticks (or any other characters that you need PowerShell to interpret).

"FirstLine`r`nSecondLine" prints
FirstLine
SecondLine

'"FirstLine`r`nSecondLine"' prints
"FirstLine`r`nSecondLine"
like image 143
Adil Hindistan Avatar answered Sep 14 '26 15:09

Adil Hindistan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!