The PowerShell Here-String – Preserve text formatting

Like many other high-level languages, PowerShell gives us the ability to preserve line breaks, tabs, whitespace, and other text formatting with the here-string. I'm going to be looking how you create and use a here-string in PowerShell.

Many other languages use the here-string, and it is not PowerShell specific. Other languages call the here-string by a different name: here-document, here-text, heredoc, and hereis to name a few. Unix shells like bash and zsh first used here-documents. Languages such as PHP, Python, and Ruby have also adopted their own "here-document."

Here-string syntax ^

To define a here-string in PowerShell, you need to use the @ character followed by either a single or double quote and then a newline. To end, close the quotes and add another @ on a newline, so it looks like:

Here string example start

Here string example start

Once you have created this here-string enclosure, you are free to add newlines, symbols, and text formatting, with no restriction on the amount of text written. PowerShell uses the .NET System.String type, which represents text as a sequence of UTF-16 code units. The advantage to using 16-bit Unicode is that it has a character set capable of encoding all 1,112,064 valid code points. So you can use characters from every language in the world!

Here-strings may be either literal (single-quoted) or expanding (double-quoted), just like regular strings in .NET. If you use single quotes with the here-string, it will not expand any variables and subexpressions. The best use for the single quote option is for string text not required to change.

Using the here-string ^

We have spoken about the history of the here-string and how it works. Now is a good time to see some working examples using the here-string in PowerShell.

We now know the here-string can preserve the formatting. Here is an example of this:

Preserved formatting with here string

Preserved formatting with here string

In this example, we used a literal here-string. Here is a something a bit more interesting, the expanding here-string:

Expanding here string example

Expanding here string example

I set a variable $name outside the here-string and added to the first line inside the here-string to show variable expansion. The second line demonstrates the use of a literal string and lastly the use of a subexpression to get the current date and time. I added the date and time to a newline with a tab indentation by using the escape characters `n and `t.

Another useful technique for the here-string is the redirection to a file. By either piping to Out-File or using the redirection operator >, you can create a script or text file:

Using redirection to create files

Using redirection to create files

Following from the previous example of creating code files from a here-string, you can also perform code generation while preserving the code formatting. I've used the here-string in this example with the Add-Type cmdlet to generate a small C# class:

Creating C# by using a here string

Creating C# by using a here string

The TypeDefinition parameter of Add-Type allows me to use a here-string containing my C# code. The C# code itself is a simple class that uses a static method to add two numbers together.

Although very simple, the here-string is a flexible and useful way to work with larger blocks of text. The examples I've shown in this article give some good cases of how you can use the here-string in your PowerShell code.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account