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.
End of Article