Expanding variables and subexpressions in strings

In PowerShell, you can define a string with single or double quotes. There is a difference between these two methods. In a single-quoted string, variables and subexpressions are not expanded, whereas, in a double-quoted string, they are expanded.

Let's look at an example of variable expansion in a double-quoted string:

PowerCLI C:\> $Number = 3
PowerCLI C:\> "The number is: $Number"
The number is: 3

In the preceding example, the string is defined with double quotes, and the $Number variable is expanded. Let's see what happens if you use single quotes:

PowerCLI C:\> $Number = 3
PowerCLI C:\> 'The number is: $Number'
The number is: $Number

Using a single-quoted string, PowerShell doesn't expand the ...

Get Learning PowerCLI - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.