Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Tuesday, December 30, 2008 11:36 AM

PowerShell_2 Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "H".  For "H", I'm going to talk about a feature that PowerShell Guru Jeffrey Snover loves talking about each time I see him: Here-Strings.

WishYouWereHere Before we get to here-strings, it's probably a good idea to go over the different kinds of string literals in PowerShell.  There are actually 4 different kinds of string literals - single-quoted strings, double-quoted strings, single-quoted here-strings, and double-quoted here-strings.  All of these types are stored in the same internal format of 16-bit Unicode characters and are directly implemented by the .NET System.String type.

Some additional features that strings have due to their System.String base, is that they can be arbitrarily long and that they are immutable, meaning that the contents can be copied but cannot be changed.

Single and Double-Quoted strings

Literal strings can contain any character with the exception of an unquoted closing quote character. 

For double-quoted strings, to embed a closing quote character, you have to either quote it with a backtick character (`) or provide two adjacent quotes which become a single literal quote in the string.  Also, for variable substitutions, only double-quote strings will process those substitutions.

In single-quoted strings, providing double quotes is the only way to embed a literal quote in the string.  In single-quoted strings, the backtick is not treated as a special character so it cannot be used for escaping newlines or other special characters.  In single-quoted strings, variable substitution is not performed.

Here are some examples of embedded quotes and variable substitutions:

PS> $s = "My name is `"Joe"", what's yours?"
PS> $s
My name is "Joe", what's yours?
PS> "He said: $s"
He said: My name is "Joe", what's yours?
PS> 'He said: $s'
He said: $s

Here-Strings

A here-string is used to embed large chunks of text inline in a script into a single string literal.  This is very similar to a feature in C#, but the difference is that in PowerShell the here-string begins at the end of a line and the terminating sequence must be at the beginning of a line.  In C#, the string terminates with the first non-doubled closing quote.

PS> $a = @"
one
two
"@
PS> $a
one
two

Here strings have the following special quoting rules: First, here-strings begin with @<quote><newline> and here-strings end with <newline><quote>@.  The content of the here-string is all of the lines between the beginning and ending quotes, but not the lines the quotes are on.  Because of these rules, other special characters such as quotes that cause problems with regular strings have no problems.

Here is an example from Windows PowerShell in Action by Bruce Payette:

PS> $a = @"
One is "1"
Two is '2'
Three is $(2+1)
The date is "$(Get-Date)"
"@ + "A trailing line"
PS> $a
One is "1"
Two is '2'
Three is 3
The date is "12/30/2008 11:31:35"A trailing line

Here-strings come in very useful when writing long scripts that contain text output you would normally have to write on multiple output statements.



Feedback

7/29/2009 11:02 PM
Gravatar great information..
social bookmarking dofollow
7/20/2010 6:33 PM
Gravatar For me it's a great post...........
uggs sale online
10/29/2010 5:30 AM
Gravatar online casinos
I've never seen this particular development site, should be helpful if it lives up to the name cos I like to do my own bita coding here and there even though I can't do it all. Usually I try webdeveloper.com just to note their presence here too. Thanks
shgama

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 1 and 5 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or