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:
Now, the following command exercises the $name variable in a string instance:
Moreover, the variable in the string can be called in the following way as well:
Another possibility of using a variable in a string expression is described below.
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.
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.
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.
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:
And the $price variable is used in the following string:
For instance, if you want to add a special character like “$” to the variable, you must use the escape character “`” (Grave accent).
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.
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:
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.