String Interpolation in PowerShell

String Interpolation in PowerShell is the way of replacing the value of a variable into placeholders in a string. It displays the value of the variable in the string.

PowerShell string interpolation provides a more readable, easier-to-read, and convenient syntax to create a formatted string.

This article will explain how to use string interpolation in PowerShell with examples.

String Interpolation in PowerShell

Let’s understand with an example to connect string using string interpolation in PowerShell.

$company = "ShellGeek"

echo "Welcome to $company"

In the above PowerShell script,

$company variable contains ShellGeek string value.

In the next statement, inside double quotes, PowerShell interpolates the string variable name and displays the result as below

Welcome to ShellGeek

If we put a variable name in a single quote, it will display the variable name instead as shown below

$company = "ShellGeek"

echo 'Welcome to $company'

it will echo string with variable name as below

Welcome to $company

String Interpolation for Environment Variable

Let’s consider an example to interpolate environment variable with its value in the string as below

echo "My computer name: $env:COMPUTERNAME"

In the above PowerShell script,

$env: COMPUTERNAME in double-quotes display a variable value and concatenate it with a string.

The output of the above script as below

My computer name: incorp-eu-101

String Interpolation with escape character

There are certain cases where putting a variable inside double-quotes doesn’t provide results as expected.

Let’s consider an example to display PowerShell course price value as below

PowerShell course value: $99

$courseValue = 99
echo "PowerShell course value $$courseValue"

In the above PowerShell script,

$courseValue inside double-quotes don’t give desired expected output because of $$

To get desired output, use PowerShell escape character (`) for string interpolation as shown below

$courseValue = 99
echo "PowerShell course value `$$courseValue"

The output of the above script as below

PowerShell course value $99

Let’s take another use case for string interpolation in PowerShell to display the current user’s home directory as below

$Home
"$Home: is User Home Directory Path" 

In the above PowerShell script,

$Home variable gets the current user’s home directory.

In the next statement, we have used $Home inside a double quote, still, it doesn’t give desired output and throws an error message as below

At line:1 char:2
+ "$Home: is User Home Directory Path"
+  ~~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

The reason for the error is a colon (:) followed by $Home variable name

To get desired output, use PowerShell escape character (`) with variable name as below

"$Home`: is User Home Directory Path"  

The output of the above script as below

C:\Users\shelladmin: is User Home Directory Path

Conclusion

I hope the above article on string interpolation in PowerShell is helpful to you.

String interpolation provides easy to read and interpret the string easily.

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

Leave a Comment