Executive Summary:
Windows PowerShell cmdlets often include strings. The rules that govern how to use quotes in PowerShell strings are referred to as quoting rules. In this lesson, you’ll learn about these rules. You also learn how to obtain and use the methods and properties of PowerShell's System.String object.
|
Most PowerShell statements include string values.
Usually, these strings are passed to cmdlets
as arguments. In some cases, the strings
are enclosed in single quotes. In other cases,
they’re enclosed in double quotes. And sometimes
they’re not enclosed in quotes at all. It’s
important to understand how to properly handle strings. The rules
that govern how to do so are often referred to as quoting rules. In
this lesson, you’ll learn about these rules. Specifically, you’ll learn
when to enclose string values in quotes and whether to use single
or double quotes. In addition, you’ll learn how to flag, or escape,
special characters.
Working with String
Values
Whenever you enclose text in quotes,
PowerShell treats that text as a string
value. So, as long as the text doesn’t contain
any special characters (you’ll learn
more about these characters shortly)
or reference variables (I’ll discuss how
to reference variables in strings in the
next lesson), you can enclose the text
in either single or double quotes. For
example, the following statements
achieve the same results:
Write-Output “String in quotes.”
Write-Output ‘String in quotes.’
In these examples, the Write-Output cmdlet sends a string object
down the pipeline or, in this case, directly to the PowerShell console.
As you can see in Figure 1, the outputted value is the same for both
statements.
In addition to the Write-Output cmdlet, PowerShell’s Out-Host
and Write-Host cmdlets output information to the console. Their differences
lie in the details. For example, the Write-Output cmdlet sends
output down the pipeline to the next cmdlet. When Write-Output is
the last cmdlet in the pipeline, the output is displayed in the console.
The Out-Host cmdlet sends output directly to the
console and offers an optional parameter that lets
you view the output one screen at a time, which
can be helpful if there is a lot of output. This is
the default output cmdlet, so if you don’t specify
an output cmdlet, Out-Host cmdlet is used. The
Write-Host cmdlet also sends output directly
to the console. However, Write-Host has two
optional parameters that let you change the color
of the text or text background, thereby creating a
customized console.
For basic quoted string values that you want
to output directly to the console window, all three
cmdlets behave in similar ways. For example, the following four commands all display the
string in the console window in the same way
as in Figure 1:
“String in quotes.”
Write-Output “String in quotes.”
Write-Host “String in quotes.”
Out-Host `
-InputObject “String in quotes.”
Notice that no cmdlet is specified in the first
command. As a result, the Out-Host cmdlet
is used.
For many of the examples in this lesson, I
use the Write-Output cmdlet because it outputs
an object in a way similar to many other
cmdlets. This lets me demonstrate different
principles about quoted values. Keep in
mind, however, that the Write-Output, Out-
Host, and Write-Host cmdlets can behave
differently in different circumstances. For
more information about these cmdlets, see
their Help files.
If you want to include quotes within
a string, you can use single quotes within
double quotes or double quotes within single
quotes:
Write-Output “String ‘in’ quotes.”
Write-Output ‘String “in” quotes.’
If you refer again to Figure 1, you’ll see that
inside quotes in both cases are carried to the
output. This isn’t the case when you use the
same type of quotes throughout the string:
Write-Output “String “in” quotes.”
Write-Output ‘String ‘in’ quotes.’
As Figure 1 shows, the results are quite different.
In both cases, the quotes are not
displayed and a new line is added. This is because PowerShell
interprets the one
string as multiple
strings and consequently
adds a line
break. For example,
PowerShell interprets
String as the
first string (so it adds
a line break after
that string), then
interprets the rest
as a different string.
You can use double
quotes within double
quotes, but you must
escape the inside
quotes, which I’ll describe how to do later.
Whenever you work with quotes, be
careful not to mix up the type of quotes or
forget to include one. Otherwise, you might
get stuck in a loop that continues to prompt
you for an entry—but nothing you enter gets
you out of the loop. If you run into that situation,
press Ctrl+C to return to the command
prompt.
Another issue to take into account when
defining cmdlet arguments is how Power-
Shell treats numerical values. As I said earlier,
PowerShell treats any values within quotes as
strings, even if the value consists of all numbers:
Write-Output “123”
When you execute this statement (shown
in Figure 2), the value returned is a string object, as Figure 2 shows. You can verify the
value’s type by running the statement:
(Write-Output “123”).GetType()
Continue on Page 2
This statement uses the GetType method
of the object’s type, which in this case is
String, or more specifically System.String,
also shown in Figure 2. For more information
about System.String and GetType, see the
sidebar “Getting and Using the System.String
Object’s Members.”
If you don’t enclose a numerical value
in quotes, PowerShell treats the value as a
numerical object. For example, the following
statement returns an integer object:
Write-Output 123
Again, you can verify the object’s type by
using the GetType method:
(Write-Output 123).GetType()
As you can see in Figure 2, the object’s type
is Int32.
If a value includes both numbers and letters,
PowerShell treats it as a string, whether
or not it’s in quotes. For example, the following
statement returns a string, as verified by
the second statement:
Write-Output 123output
(Write-Output 123output).GetType()
Once again, Figure 2 shows the output of
these statements.
In most cases, you can omit quotes if
your argument is a string with no embedded
spaces. For example, the following three
statements use the Set-Location cmdlet to set
the working folder to the C drive:
Set-Location C:\
Set-Location “C:\”
Set-Location ‘C:\’
Now suppose you want to change the working
folder to C:\Documents and Settings:
Set-Location `
C:\Documents and Settings
This statement generates an error because
it doesn’t know what to do with the tokens
(words) after the first space. As you can see in Figure 3, the parser interprets “and” as a
parameter, and because there’s no parameter
by this name, the parser generates an error.
You can easily fix this problem by enclosing
the entire argument in quotes:
Set-Location `
“C:\Documents and Settings”
Now when you run this statement, it changes
the working folder, as shown in Figure 3.
One other important issue when working
with strings is how to reference variables
within a quoted string. If you enclose a
string in double quotes, the variable’s
value is used. If you enclose a string in
single quotes, the literal value is used. As I
mentioned earlier, I’ll cover variables and
describe how to reference them in strings
in the next lesson. However, if you’re anxious
to learn more about using variables in
your strings now, refer to the about_quoting_
rules Help file.
Escaping Special Characters in Strings
Up to this point, the arguments you’ve seen in the examples could have
taken single or double quotes. As a
result, it would appear that there’s
no difference between the two.
However, there’s one very important
difference: Single quotes always
treat a string literally, whereas double
quotes allow you to escape special
characters within the text. A
special character is one that, when
preceded by a backtick (`), takes
a specific action that it would not
have taken without the backtick. Table 1 lists PowerShell’s special
characters.
The best way to explain this concept
is through a few examples. In
the following statement, several characters
have been escaped in order to change how
the text is displayed:
Write-Output (“`n`tText includes” + `
“`n`t`”escaped`” characters.`n”)
The first escaped character (the one preceded
by the first backtick) is n, which in
this context inserts a new line. The next
escaped character is t, which inserts a tab.
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 (see Lesson 2). In the second line, `n and `t are used several more
times. In addition, backticks
precede the double quotes surrounding
the word escaped. As a
result, the double quotes appear
in the output. If you refer to Figure 4, you’ll see how the new
lines, tabs, and double quotes
appear. For more information
about escaping characters, see
the about_escape_character
Help file.
If you try to escape characters in a string
enclosed in single quotes, the backtick and
special characters have no effect on the
output, other than to be treated literally.
For example, the statement
Write-Output ‘`tindented`n`twords’
returns the exact string as originally typed,
as Figure 4 shows. Note that, in pre-release
versions of PowerShell, you could escape
characters inside single-quoted strings, and
PowerShell would correctly parse them. So,
you might come across material that says
that this is how PowerShell handles singlequoted
strings.
Moving Forward
In most cases, string values play an important
role in creating PowerShell statements. The
better you understand how to work with
strings, the more effective your statements
will be. I encourage you to spend time practicing
the various ways to use strings. Try
enclosing them in single and double quotes,
then try running the commands without the
quotes. And don’t forget to practice escaping
special characters.