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 string with single quotes, variables and subexpressions are not expanded; while in a string with double quotes, variables and subexpressions 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, ...

Get Learning PowerCLI 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.