When you write a
script, particularly a system administration script, you rarely get
to hard-code in all your values ahead of time; instead, you
typically need to retrieve information, store that information in a
variable or two, and then display the values of those variable. For
example, you might have a simple VBScript script that uses code
like this to grab data from Active Directory:
Once you have that
data you’d then like echo back a nice little sentence like
"Ken Myer is an Accountant
in Financial Services."
No big deal, right?
Well, maybe. On the other hand, combining text and variable values
can be a bit tricky in VBScript, to say the least. For example, to
echo the above information you’ll have to write a line of code that
looks like this:
Wscript.EchostrFirstName& " " &
strLastName& " is an " &
" in" &
impossible, but it
canbe cumbersome, and it’s easy to do things like, say,
leave out a blank space. In turn, those simple mistakes result in
somewhat less-than-professional output, output similar to this:
"Ken Myer is
But what are you
going to do about it? After all, it’s not as if you can directly
embed a variable within a string. Like it or not, all those
ampersands and double quote marks are just an unfortunate fact of
Well, unless you’re
using Windows PowerShell, that is.
Displaying Variable Values in
PowerShell you actually
canembed a variable within a string, and PowerShell will
valueof that variable. What does that mean? Well, consider
this simple little PowerShell script, one that assigns values to
four different variables and then uses the
Write-Hostcmdlet to echo back a string that includes each of
an$Title in $Department."
Now, what do you
suppose will actually get displayed to the screen? Believe it or
thisis what gets displayed to the screen:
Ken Myer is an Accountant
in Financial Services.
No ampersands, no
double quote marks, no need to worry about adding blank spaces:
just include the variable within the string and PowerShell will
echo back the value of the variable.
It’s so simple and
obvious that it’s revolutionary.
there might be times when you
don’twant the value of a variable to be displayed to the
screen. For example, suppose you wanted
thisdisplayed to the screen:
an$Title in $Department.
Weird, but it’s none
of our business what you want to echo back to the screen.
So how are you
supposed to do that if PowerShell automatically replaces variables
with their values? That’s easy; you just need to enclose the string
an$Title in $Department.'
Single quote marks
result in literal values being echoed back; double quote marks
result in the actual value of a variable being echoed back.
Here’s another one
for you. As you know, PowerShell includes a number of “automatic”
variables that return information about PowerShell and the
PowerShell environment; for example, the $
pshomevariable returns the name of the
folder where Windows PowerShell was installed. Suppose we have this
line of code (it’s a weird line of code, but bear with us):
That’s going to
return the value of $
pshome; that is, the folder where
PowerShell was installed:
Now suppose we have
thisline of code:
Thanks to the single
quote marks that’sgoing to return the
following string (literal) value:
Needless to say,
that’s all great.
But what if we wanted to echo back the
The value of the $
How in the world are
we going to do
that? How can we echo back both the literal and the actual
value of a variable, and in the same sentence to boot?
Write-Host "The value of
pshomevariable is $
See what we’ve done
here? We’ve enclosed the entire string in double quote marks;
therefore you’d expect PowerShell to echo back the
valueof each instance of $
pshome. But notice the funny little
character (the “grave accent”) we placed in front of the first
instance of $
That’s the secret
right there: put a grave accent in front of a variable and
PowerShell will echo back the variable name ($
pshome) rather than the value of that
variable. And that’s true even if – as we’ve done here – the entire
string is enclosed in double quotes.
In other words,
combining variables and other text is easy is PowerShell: just
place the variables within a string and PowerShell will, by
default, echo back the value of those variables. Admittedly, we’re
going to miss ampersands, double quotes, and weird constructions