PowerShell For Programmers: Here Strings, There Strings, Everywhere Some String Strings

Kory Thacher

There won’t be much code in today’s post, but this can be a useful feature to know about.

In addition to the expandable and literal strings we talked about, we can also use something called a Here String. Here strings allow us to have quote characters inside of our string that match the quote characters we use to create that string.

For example, you might want the double quote character to appear inside of your expandable-string. You could use the escape character on the internal quote to prevent it from closing your string, but if you had a massive amount of quotes this would be annoying. Here strings exist to make this process easier (and if you every copy and paste a big chunk from a book you’ll be glad to not have to worry about escaping).

Here strings can be used with both single and double quotes, when you want a literal or expandable version. 

To use a Here String we need to do the following:

  1. Jam @ symbols around the quotes
  2. Line break after opening the quote
  3. Close the quote on a line all by itself as the first and second characters
$here = @"
Some "stuff" inside my expandable here string

$here2 = @'
Some 'stuff' inside my literal here string

Try breaking some of these rules and see how the ISE reacts. Notice if you forget to line break at the beginning it will help you out with:

No characters are allowed after a here-string header but before the end of the line.

However, if you forget to put the closing “@ on the last line as the first and second characters you get a much less helpful (and much more frustrating) error if you never learned the above rules:

The string is missing the terminator: “@.

Here strings can be useful for:

  • Outputting a large chunk of string data on multiple lines that might otherwise take multiple output statements. I’ve never had an issue just doing it with a normal expandable string + line breaks however.
  • Copying in a large chunk of data from a file, email, excel, etc that may or may not have quotes in it.


I’ve found that sometimes when I use normal, expandable strings, and I manipulate the data and call export-csv on the results that it will have hidden excess quote characters. I found this when my file was rejected by an internal tool with that error. Changing the same code to an expandable-here string actually didn’t have the same issue. I’m guessing this has to do with the way it generates the string literal, but I couldn’t dig up any specific info on it.


Well that’s all for now, hopefully this helps you on your PowerShell journey and saves you a bit of frustration working with here strings. I’ll be adding to the series to cover other quirks as well so let me know in the comments if there is any particular topics confusing you!

For the main series post, check back here.

As always, don’t forget to rate, comment and share! Let me know what you think of the content and what topics you’d like to see me blog about in the future.


Discussion is closed.

Feedback usabilla icon