Powershell

How to perform PowerShell String Interpolation

PowerShell provides an extensive list of functions and cmdlets that contains distinctive features and supports all the basic operations that a user can perform using GUI. PowerShell does support a scripting language that can be practiced performing some fundamental tasks of a programming language.Using PowerShell, you can interpolate a literal string containing one or more placeholders. The output of that command replaces the placeholder with the value it contains. Many programming languages make use of string interpolation or variable interpolation. In this descriptive post, the working mechanism and usage of PowerShell’s string interpolation concept is provided.

How PowerShell String Interpolation works

Generally, most of PowerShell’s operations are carried out using any cmdlet or function and therefore they do have some syntax to follow. The String Interpolation is a process that manipulates the string operations therefore it does not have any dedicated syntax. The basic functioning of the string interpolation concept is carried out in the following way:

  • Declare a variable: $var="<abc>"
  • Use that variable in any string expression like: "$var" or "$($var)" or "${var}"

And the $var placeholder will be replaced by its value in the string expressions.

The following examples in this guide would provide a better explanation of PowerShell string interpolation.

Example 1 : Using String Interpolation to expand a string

This example comprises two commands that serve the following purposes:

A variable $name is declared and the string “linuxhint” is stored in $name:

> $name="linuxhint"

Graphical user interface Description automatically generated with low confidence

Now, the following command exercises the $name variable in a string instance:

> Write-Output "$name provides a rich tech related content"

Moreover, the variable in the string can be called in the following way as well:

> Write-Output "$($name) provides a rich tech related content"

Another possibility of using a variable in a string expression is described below.

> Write-Output "${name} provides a rich tech related content"

It is mandatory that you must call the string variable in a double quote instance. If you try to use a string variable in single quotes, then it would print the variable itself(instead of fetching the value stored in that variable). For instance, we have called the $name variable in a single quotation and the output prints the $name variable.

> Write-Output '$name provides a rich tech related content'

Example 2: Using scripts for PowerShell String Interpolation

This example describes the string interpolation concept using PowerShell scripts. For a PowerShell script, open the “PowerShell ISE“.

The steps are the same here as well:

Firstly, declare a variable as we have named it in $svar. And the Write-Host cmdlet is used to call the $svar variable in a string.

Graphical user interface, application, Word Description automatically generated

After doing so, press CTRL+S to save the script with any name and location, we have named it “pssi”.

Now, run the script in the PowerShell console by giving its complete path (where you have saved it).

In our case, it is saved at C:\scripts\pssi.ps1 so, we will put the same in our console and hit enter to execute the script.

> C:\scripts\pssi.ps1

A picture containing text, indoor, dark Description automatically generated

Example 3: Using numeric variables in strings

The above examples denote the interpolation of string variables in string instances. Here, we have created a variable that contains a numeric value:

> $price=100

A picture containing text Description automatically generated

And the $price variable is used in the following string:

> Write-Output "The price of the novel is $price"

Text Description automatically generated with low confidence

For instance, if you want to add a special character like “$” to the variable, you must use the escape character “`” (Grave accent).

> Write-Output "The price of the novel is `$$price"

Example 4: Using environment variables

The environment variables are predefined variables of an operating system that contain some information about your machine. The command written below writes the string on the screen and fetches the environment variable that shows the HOMEPATH of your PC. As the HOMEPATH environment variable is pre-defined, therefore the variable declaration is skipped.

> Write-Output "The home path of my PC is: $env:HOMEPATH"

Note: The “$env:<name-of-env-variable>” is the standard syntax to call an environment variable. Moreover, you can get the list of environment variables by using the command provided below:

> dir env:

Conclusion

PowerShell provides various cmdlets and functions for different operations. The string interpolation comes into action when you want to append a variable (using its name) to a string instance. This article provides a brief explanation of doing PowerShell string interpolation. You have learned to get an understanding of string interpolation on user-defined and built-in variables. The working pattern is the same across various situations, but the variable must be inserted carefully to avoid any errors.

About the author

Adnan Shabbir