Modified: 01/19/2015

Variable Interpolation, or Two Types of Strings

Strings with single quotes (') are literal strings, meaning there are no special characters; every character in the string will output as itself. Strings with double quotes (") in powershell will expand variables and escape characters inside the string; this is referred to as interpolation.

The following code will expand $text in the string:

$text = "John Doe"
"Hello, my name is $text.`r`nThis is a new line."

Output:

Hello, my name is John Doe.
This is a new line.

However, this code will not expand $text:

'Hello, my name is $text.`r`nThis is a new line.'

Output:

Hello, my name is $text.`r`nThis is a new line.

When attempting to expand a property of an object, a key of a hashtable, or an index of an array, you must enclose the variable in a subexpression ($()).

Here are some examples:

$object = Get-Item -Path .
"Here is the name of the object: $($object.Name)"

$hashtable = @{'Key'='Value'}
"Here is the value of Key in the hashtable: $($hashtable['Key'])"

$array = @('One','Two','Three')
"Here is the second item in the array: $($array[1])"

If you fail to enclose these types of references in a subexpression, you will get some unexpected results.

Variable interpolation works normally when accessing variables in PS drives like the environment variable drive. Take the following code:

"The system root is $env:SystemRoot."

Output:

The system root is C:\Windows.

Nested Quotations

Strings that are wrapped in double quotes (") can have single quotes (') inside them and vice versa. If you need to have a double quote as part of a string enclosed in double quotes, you can escape the double quote. You can also put two consecutive double quotes; which has the same effect as escaping the quotes. The only way to include single quotes inside a single quoted literal string is to use double single quotes. Double single quotes inside a single quoted string will be interperitted as one single quote. This is the only non-literal interpritation, that I know of anyway, that happens inside a single quoted string.

"A `"string`" with escaped inner double quotes"
"A ""string"" with dual inner double quotes"
'A ''string'' with dual inner single quotes'

Output:

A "string" with escaped inner double quotes
A "string" with dual inner double quotes
A 'string' with dual inner single quotes

Modified: 01/19/2015

Variable Interpolation, or Two Types of Strings

Strings with single quotes (') are literal strings, meaning there are no special characters; every character in the string will output as itself. Strings with double quotes (") in powershell will expand variables and escape characters inside the string; this is referred to as interpolation.

The following code will expand $text in the string:

$text = "John Doe"
"Hello, my name is $text.`r`nThis is a new line."

Output:

Hello, my name is John Doe.
This is a new line.

However, this code will not expand $text:

'Hello, my name is $text.`r`nThis is a new line.'

Output:

Hello, my name is $text.`r`nThis is a new line.

When attempting to expand a property of an object, a key of a hashtable, or an index of an array, you must enclose the variable in a subexpression ($()).

Here are some examples:

$object = Get-Item -Path .
"Here is the name of the object: $($object.Name)"

$hashtable = @{'Key'='Value'}
"Here is the value of Key in the hashtable: $($hashtable['Key'])"

$array = @('One','Two','Three')
"Here is the second item in the array: $($array[1])"

If you fail to enclose these types of references in a subexpression, you will get some unexpected results.

Variable interpolation works normally when accessing variables in PS drives like the environment variable drive. Take the following code:

"The system root is $env:SystemRoot."

Output:

The system root is C:\Windows.

Nested Quotations

Strings that are wrapped in double quotes (") can have single quotes (') inside them and vice versa. If you need to have a double quote as part of a string enclosed in double quotes, you can escape the double quote. You can also put two consecutive double quotes; which has the same effect as escaping the quotes. The only way to include single quotes inside a single quoted literal string is to use double single quotes. Double single quotes inside a single quoted string will be interperitted as one single quote. This is the only non-literal interpritation, that I know of anyway, that happens inside a single quoted string.

"A `"string`" with escaped inner double quotes"
"A ""string"" with dual inner double quotes"
'A ''string'' with dual inner single quotes'

Output:

A "string" with escaped inner double quotes
A "string" with dual inner double quotes
A 'string' with dual inner single quotes

Next Post