String Interpolation in PowerShell

  1. What Are Variables in PowerShell?
  2. String Interpolation in Windows PowerShell
  3. String Interpolation With Environment Variables in PowerShell
  4. String Interpolation With Escape Characters in PowerShell
  5. String Interpolation Using Variables With Special Characters in PowerShell
  6. String Interpolation Using a Subexpression Operator in PowerShell
String Interpolation in PowerShell

String Interpolation in Windows PowerShell replaces the value of a variable into placeholders in a string. Also, the process of String Interpolation displays the value of the variable in the string.

Windows PowerShell string interpolation provides a more readable, easier-to-read, and convenient syntax to create a formatted string. This article will discuss using variables during string interpolation in Windows PowerShell.

What Are Variables in PowerShell?

Before discussing Windows PowerShell strings interpolation, we should first discuss one of the critical requirements to achieve this: the Windows PowerShell variables.

As written in the Microsoft official documentation, a variable is a unit of memory in which values of many data types are stored. In Windows PowerShell, variables are represented by strings that begin with a dollar sign $.

$my_var = "Hello World!"

We will use these variables to perform String Interpolation in Windows PowerShell.

String Interpolation in Windows PowerShell

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

Example Code:

$company = "XYZ Company"
Write-Output "Welcome to $company"

In the above PowerShell script, $company variable contains XYZ Company string value.

Inside double quotation marks "", Windows PowerShell interpolates the string variable name and displays the result below in the following statement.

Output:

Welcome to XYZ Company

If we put a variable name in a single quotation mark '', it will display the variable name instead, as shown below.

Example Code:

$company = "XYZ Company"
Write-Output 'Welcome to $company'

Output:

Welcome to $company

This output happens because anything enclosed with a single quotation mark treats anything inside as a literal expression. So, make sure that you are using double quotation marks when interpolating a string.

String Interpolation With Environment Variables in PowerShell

Environment variables, depicted by the Env: variable in Windows PowerShell, store the operating system environment and programs. This information details include the operating system path, location of the Windows installation directory, number of processes used by the operating system, and so much more.

In Windows PowerShell, environment variables can be interpolated inside a string like other standard variables.

Example Code:

Write-Output "My computer name: $Env:COMPUTERNAME"

In the PowerShell script above, $Env: COMPUTERNAME displays a variable value in double quotation marks and concatenates it with a string.

Output:

My computer name: WINDOWS-PC01

String Interpolation With Escape Characters in PowerShell

Despite discussing that we should use double quotation marks during string interpolation, certain cases where putting a variable inside double quotation marks doesn’t provide results as expected. One example of this is escape characters.

An escape character is a kind of character that invokes an alternative interpretation of the following characters in a character sequence. Escape characters are common among programming languages as certain symbols are used when compiled programmatically.

For example, in Windows PowerShell, a dollar sign $ is one of the most common operators used to define a variable. We will use the dollar sign as an example below.

Example Code:

$price = 99
Write-Output "Discounted Price: $$price"

In the Windows PowerShell script above, $price inside double quotation marks doesn’t give the expected output because the scripting environment interprets double dollar signs $$ differently.

Output:

Discounted Price: Discounted Price: $$priceprice

Use Windows PowerShell escape character for string interpolation below to get the desired output.

Example Code:

$price = 99
Write-Output "Discounted Price: `$$price"

Output:

Discounted Price: $99

String Interpolation Using Variables With Special Characters in PowerShell

As mentioned, variable names that begin with a dollar sign $ can include alphanumeric characters and special characters.

The best practice of defining variables is only to include alphanumeric characters and the underscore _ character. Variable names that include other special characters, including spaces, are difficult to use and should be avoided.

However, we are not saying that using variables with special characters is impossible. If the situation can not avoid this, we can use the dollar sign and curly brackets ${} to escape all special characters, including spaces, in a variable name.

Example Code:

${this is a variable!} = "Hello"
Write-Output "${this is a variable!} World!"

Output:

Hello World!

String Interpolation Using a Subexpression Operator in PowerShell

In Windows PowerShell, we can use the subexpression operator $() to run an expression within an expression like string interpolation. We do this by enclosing whichever expression we run with a dollar sign and parentheses ().

Example Code:

$num1 = 10
$num2 = 5

Write-Output "$num1 + $num2 = $($num1+$num2)"

Output:

10 + 5 = 15
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell String