Home ยป PowerShell ยป PowerShell Variable Expansion

PowerShell Variable Expansion

In PowerShell variable expansion allows you to embed the value of a variable within a string. Double-Quotes enable variable expansion. Variable expansion in string enables dynamic string creation based on the variable values.

In this article, we will discuss variable expansion in PowerShell and different ways to achieve expanded variables in the string.

Use Double-Quote Strings for PowerShell Variable Expansion

Double quotes strings allow you to embed variable values within the string.

$userName = "ShellAdmin"
$greeting = "Hello, $userName!"

In the above PowerShell script, the $userName variable stores the value of the data type string. In the next command, within double quotes, it enables variable expansion and creates a dynamic string based on variable value.

The output of the above PowerShell script is:

Hello, ShellAdmin

Can we use Single-Quote Strings for Variable Expansion?

Single-Quote strings do not allow variable expansion, consider the contents as a literal string.

$userName = 'ShellAdmin'
$greeting = 'Hello, $userName!'

In the above PowerShell script, the single quotes treat the content as plain text. Hence the output of the above script is:

Hello, $userName

Cool Tip: How to use variables in single quotes in PowerShell!

Escaping Characters in Variable Expansion

If a string contains the literal dollar sign or other special characters in a double-quoted string, use the backtick escape operator to enable them in variable expansion.

$bookValue = "The price of PowerShell book is `$25."

The output of the above PowerShell script is:

PS C:\> $bookValue = "The price of PowerShell book is `$25."                                                            PS C:\> $bookValue                                                                                                      
The price of PowerShell book is $25.
PS C:\>                                                                                                                               

Use SubExpressions in Double-Quoted String

Subexpressions allow you to embed the result of an expression within a double-quoted string.

$radius = 4
$areaofCircle = "The area of circle is $(3.14 * $radius * $radius)."

The output of the above script is:

PS C:\> $radius = 4                                                                                                     
PS C:\> $areaofCircle = "The area of circle is $(3.14 * $radius * $radius)."                                            PS C:\> $areaofCircle                                                                                                   

The area of circle is 50.24.
PS C:\>                                                                                                                                   

Conclusion

I hope the above article on PowerShell variable expansion using double quotes is helpful to you.

PowerShell variable expansion provides enhanced flexibility in your scripts, allowing you to create dynamic strings based on the variable value.

Variable expansion is commonly used to generate customized output messages and construct file paths or URLs based on variable values.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment