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, PowerShell doesn't expand the $Number
variable. Let's try to put the number of virtual CPUs of a virtual machine in a double-quoted string:
PowerCLI C:\> $vm = Get-VM -Name dc1 PowerCLI C:\> "The number of vCPU's of the vm is: $vm...