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: , , ,

Related posts:

  1. How to deal with long filenames in Windows Powershell
  2. How to schedule a Windows Powershell script

TAGS:

5 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

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Recent Readers

JOIN MY COMMUNITY!
                 
                  Computers Blogs - Blog Top Sites