Referencing Variables in Strings
In PowerShell, you can use variables in
string values. However, the way in which
variables are treated at runtime depends on whether you use single or double quotes.
When you use single quotes, PowerShell
outputs the variable’s name as entered.
When you use double quotes, PowerShell
outputs the variable’s value.
For example, let’s assign the value eventlog to a string variable named $svc:
$svc = “eventlog”
You can now use that variable in your
strings. You just type the variable’s name
as you would any other word:
Write-Output "The service is $svc."
When PowerShell sees the dollar sign, it
interprets the word as a variable because
the string is enclosed in double quotes.
PowerShell then inserts the variable’s value
in place of its name, giving the result:
The
service is eventlog.
Now let’s use single quotes:
Write-Output 'The service is $svc.'
This time, PowerShell treats the variable
as a literal value and outputs the variable’s
name, giving the result:
The service is $svc.
If you want to include both the variable’s
name and value in a string, you can
use double quotes (but not single quotes)
and escape the variable you want to keep
as a literal value with a backtick (`):
Write-Output `
"The value of `$svc is $svc."
(Note that the backtick at the end of the first
line isn’t being used as an escape character
but rather as a continuation character.) The
result is:
The value of $svc is eventlog.
If you try to use single quotes
Write-Output `
'The value of `$svc is $svc.'
PowerShell outputs the string as it’s entered,
backtick and all:
The value of `$svc is $svc.
Using a Variable Alone as an Argument
In the preceding examples, you saw how
to include a variable in a string value that
was passed as an argument to the Write-
Output cmdlet. However, in some cases, you might want to use a variable as a
cmdlet argument without it being part of
a string. In that case, you can simply use
the variable as the argument. For example,
the following command uses the $svc
variable as an argument for the name of
the service:
Get-Service $svc
If you were to enclose the argument in
double quotes, as in
Get-Service "$svc"
you would receive the same result because,
as you saw earlier, PowerShell retrieves
the variable’s value when it’s enclosed in
double quotes. Also as you saw earlier,
if the variable appears in single quotes,
PowerShell will interpret the variable name
literally. As a result, the statement
Get-Service '$svc'
will fail because PowerShell will interpret
the service name as
'$svc'.
Moving Forward
Variables play a vital role in effective scripting,
whether they’re built-in, environment,
or user-defined. You’ll find them a useful
tool at the command line and in scripts.
Be sure to review the PowerShell Help files
for more information on variables, and
practice creating and using them. Use the
Get-Member cmdlet to retrieve methods
and properties and try them out. As you
move into more complex code, you’ll find
that you’ll be using variables—as well as
their methods and properties—in a wide
range of scripting solutions.