Quotes, CHAR 34, & and + – I love Here-Strings!
I'm 99% sure that some of the posts that I write are going to be teaching you Powershell experts to suck lemons. However, for us self taught VBx deserters there are a few things that we might have missed, so I thought I'd write about Here-Strings which I only came across yesterday and are set to revolutionise my scripting life!
For those of you that have spent most of your coding life using VB, it's hard to break from the mindset of concatenating strings with quotes in them using "& chr(34) &" or various implementations of """ or "" or even """"! Unfortunately, this is something I carried over into the Powershell world "assuming" that nothing had changed and set about using [char]34 instead. Fortunately, this has been updated in Powershell with the advent of Here-Strings. Have a look at this technet post for more info. To give you a quick demo, let's look at how you would write the following string in VBx:
Hi guys, welcome to my PoSH blog,
let's try using some "quotes" and see what happens.
In VB you would write something like this:
Dim MyString MyString = "Hi guys, welcome to my PoSH blog," & vbcrlf & "let's try using some " & chr(34) & "quotes" & chr(34) & " and see what happens"
as easy as that!
NOTE: You need to enter into a new line after the first @" and if you're typing this at the prompt, you'll need to enter into a new line after your text input and then enter "@ to finish your here string. If you just copy and paste the examples below, for some reason the formatting gets lost and the new lines don't work
However, in Powershell you can now do this:
$MyString = @" Hi guys, welcome to my PoSH blog, let's try using some "quotes" and see what happens. "@
You can even use in-line string variable replacement, for example:
$MyStringToReplace = "quotes" $MyString = @" Hi guys, welcome to my PoSH blog, let's try using some "$MyStringToReplace" and see what happens. "@
It's a thing of beauty! Now, I just need to go back and update all of my old code!
As a side note, if you want to see the Powershell rules on quoting run "man about_Quoting_Rules" in your next Powershell session.



