Dec 10 2007

How to put line breaks into a text string in Windows Powershell

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Google Query: line breaks in powershell

In a particular Powershell project I was working on, I needed to “Send an email using Windows Powershell.” I figured out how to do that, so the next trick was formatting the body of the email to look like more than just one long sentence.

In VBScript you would do this with VBcrlf (carriage return, line feed) and non-breaking spaces until you were satisfied with the text formatting. I was looking for something similar in Windows Powershell until I discovered that Powershell offers a more elegant solution. I found a little gem called a “here-string” at the Technet Script Center. The summary of this feature is below.

A “here-string” is basically a WYSIWYG (What You See Is What You Get) text string that you can assign to a variable. You begin the string by initiating it like the example below.
$MyVariable = @"

Now anything following this will be formatted like you type it. For example:

$MyVariable = @"
"I will be formatted with
odd
line
breaks
without
a single
line of line-break
code!
"
"@

As you can see you end the “here-string” like so:
"@
on a line by itself.

So what will the output of this variable be? Anything in between the top and bottom of the “here-string” block will be formatted as you typed it. See below:
"I will be formatted with
odd
line
breaks
without
a single
line of line-break
code!
"

It’s magic! The more I use Powershell, the more I love it! It’s efficient and elegant. A potent combination. Happy Coding!

Technorati Tags: , , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

TAGS:

10 Comments on this post

Trackbacks

  1. Array Help - GotGames.com.au wrote:

    [...] powershell is >< . I did bit of google searching.. read this it should fix your problem How to put line breaks into a text string in Windows Powershell | SearchMarked.com __________________ #res.gaming- Zer0// – owned_ – Silverbullet – Skfs4r – MaFox [...]

    June 17th, 2008 at 3:28 am
  1. Mike Lieman said:

    You really believe that’s magic?

    Learn Perl.

    December 10th, 2007 at 8:26 pm
  2. hstagner said:

    Magic relative to the crazy things that I have to go through in VBScript. Perception is always relative.

    By the way, I do agree with you. I started learning some beginning Perl and its text-parsing capabilities are light years beyond VBScript.

    I am also enjoying learning Ruby.

    Unfortunately, as a system administrator for a Windows Network, I am stuck with the default VBScript for now until I can install another language.

    Thanks for reading. I hope to see you again soon.

    December 10th, 2007 at 8:47 pm
  3. GMT said:

    hello, does variable interpolation (to borrow a perl-ism) work with this concept? something like:
    $something = “peaches”
    $var = @”
    “this, that
    the other
    and yet more…
    i saw $something
    over there

    “@

    thanks, gmt

    September 30th, 2008 at 11:43 pm
  4. GMT said:

    Yep … it works.
    I just made a goofy little test script and it formatted the text perfectly, and subsitituted the value of $something in the body of the text.

    September 30th, 2008 at 11:52 pm
  5. ASBNSG said:

    for some reason, when i test this out, all the original line breaks in my file have been removed and replaced with a unicode square(?) i’m using notepad to create the txt file, and i’m using the powershell CLI to add the text.

    is there something i’m missing here?

    February 24th, 2009 at 11:15 pm
  6. Royce said:

    Thanks for this — I applied it to another post I found here like you suggested to have a multi-line email. However, I’m trying to have some blank lines in the email, but the here-string seems to remove those. Everything else is formatted as I want, except blank lines get removed. Anyone know how to have blank lines in a here-string, or another way to solve my problem? Thanks!

    January 15th, 2010 at 6:05 pm
  7. Royce said:

    Found my own answer eventually — use `n (back-tick then n) and it will give you a blank line.

    January 21st, 2010 at 2:57 pm
  8. Josh said:

    RE: ASBNSG and unicode squares

    That happens because you are writing it into a string value instead of a multistring.

    April 13th, 2010 at 11:59 am
  9. Raheem said:

    You can also use `n to create a line break. But most people type the left apostraphe using the wrong key. The correct one is to the left of the 1 key. See code example at http://www.powershelldude.com/how-to-add-line-breaks-in-powershell/

    October 25th, 2010 at 6:09 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                  Computers Blogs - Blog Top Sites