Windows PowerShell Tip of the Week
Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.
Using Windows PowerShell “Here-Strings”
One of the problems people have when sizing up a new scripting language (like Windows PowerShell) is this: by definition, scripting languages pretty much have to have a lot in common with one another. For example, in VBScript you can do For Each loops, For Next loops, Do While loops, and Do Until loops; in PowerShell you can do the same things (albeit with slightly different syntax). VBScript lets you manage processes, services, and event logs; Windows PowerShell lets you manage processes, services, and event logs. To make matters even more confusing, Windows PowerShell even lets you call actual VBScript functions:
$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")
At some point you’re bound to wonder, “What difference does it make?”
That, needless to say, is one of the main driving forces behind our Windows PowerShell Tip of the Week. After all, the Scripting Guys aren’t trying to “sell” you Windows PowerShell; if you want to know the truth, we don’t care whether you use PowerShell, VBScript, or both (or Ruby or Python or Perl or any other scripting language). Instead, we just want to educate you on both VBScript and Windows PowerShell; after that it’s up to you to decide which language you’d prefer to use. And one facet of this education is to compare and contrast the two languages, on simple tasks as well as on more-complicated chores.
For example, consider the process by which you assign multi-line string values to a variable. In VBScript this is a somewhat less-than intuitive task; that’s because you have to deal with ampersands, double quote marks, and line break characters. For example, suppose we have the following paragraph, taken from Lewis Carroll’s Alice in Wonderland:
“Curiouser and curiouser!” cried Alice (she was so much surprised, that for the moment she quite forgot how to speak good English); “now I'm opening out like the largest telescope that ever was! Good-bye, feet!” |
How can you assign that passage (complete with the embedded quote marks) to a VBScript variable? Well, it can be done, but it’s not pretty:
x = """Curiouser and curiouser!""" & " cried Alice (she was so much surprised," & _
" that for the moment she quite forgot how to speak good English); " & _
"""now I'm opening out like the largest telescope that ever was! " & _
"Good-bye, feet!"""
Wscript.Echo x
That works perfectly well, but it’s definitely a little complicated; as the Scripting Guys can attest, the odds are pretty good that you won’t get it exactly right on your first try. And what if you later need to edit that value, say by adding another line or another set of double quote marks? Well, about all we can say is this: good luck.
So does Windows PowerShell make this any easier? We’ll let you judge that for yourself:
$x = @"
"Curiouser and curiouser!" cried Alice (she was so much surprised,
that for the moment she quite forgot how to speak good English);
"now I'm opening out like the largest telescope that ever was!
Good-bye, feet!"
"@
$x
If you’re looking for line break characters, ampersands, and triple sets of double quote marks, here’s some advice: don’t look too hard, because you aren’t going to find any. Instead of having to worry about line break characters and the like Windows PowerShell has a construction known as a here-string, a construction that lets you bypass the complexities otherwise involved in assigning a multi-line string value to a variable. As shown above, you indicate the start of a here-string by using syntax similar to this:
$x = @"
We want to assign our string value to a variable named $x; that’s why we started things off with $x = followed by @", the syntax that represents the start of our here-string. (Which, we should add, needs to be on a line all by itself.) We then indicate the end of the here-string by typing the construction "@, also on a line by itself.
The cool part, however, is what lies in-between the beginning and the end of the here-string. In between the opening line and the closing line all we have to do is type the string value exactly the way we want it assigned; in turn, PowerShell will respect any line breaks, double and single quote marks, and blank spaces we type in. Suppose we wanted to type each word and punctuation mark on a separate line. In that case, our here-string (or at least the first part of it) would look like this:
$x = @"
"
Curiouser
and
curiouser
!
"
"@
And what do you suppose will show up onscreen when we echo back the value of $x? Good guess:
"
Curiouser
and
curiouser
!
"
As you might expect, this is also a nifty way to add comments to a script: you can type as much text as you want any way you want, and without having to comment out each and every line. Here’s a comment that uses a here-string:
$x = @"
This script demonstrates the use of "here-strings."
Anything typed between the top line of this code block
and the bottom line of this code block will be formatted
exactly as shown here.
"@
And here’s a comment that doesn’t:
# This script demonstrates the use of "here-strings."
# Anything typed between the top line of this code block
# and the bottom line of this code block will be formatted
# exactly as shown here.
It’s not a huge deal, but we find the here-string version a little easier to read. And – trust us here – the here-string version is definitely easier to edit, in part because you can add line breaks anywhere you want without having to add in (or delete) the comment character.
Incidentally, you don’t have to assign a here-string to a variable; instead, you can simply insert a here-string, like so:
@"
"Curiouser and curiouser!" cried Alice (she was so much surprised,
that for the moment she quite forgot how to speak good English);
"now I'm opening out like the largest telescope that ever was!
Good-bye, feet!"
"@
What’s going to happen when you run this script? Well, because the here-string is not assigned to a variable the script will simply print out the value of that here-string, like so:
"Curiouser and curiouser!" cried Alice (she was so much surprised,
that for the moment she quite forgot how to speak good English);
"now I'm opening out like the largest telescope that ever was!
Good-bye, feet!"
Granted, this might not be the most important feature Windows PowerShell has to offer. But it’s still a feature worth knowing about.