Powershell

What does $() do in PowerShell?

In PowerShell there are different categories of operators for example, arithmetic operators, logical operators, comparison operators, etc. Apart from these operators there are some special operators that are used to achieve different functionalities such as an array subexpression operator @(), subexpression operator $(), etc. In this write-up we will learn the basic fundamentals of the subexpression operator $().

The subexpression operator $() enables us to evaluate the expression and act on the results of that expression in a single line. While working with the subexpression operator there is no need for the intermediate/third variable.

This write-up will explain the below listed aspects of array sub-expression operator:

So, let’s start!

What is $() in PowerShell

It is a special operator in PowerShell commonly referred to as subexpression operator. As the name itself suggests it is used when we have to use one expression within some other expression. For instance, embedding the output of a command with some other expression.

Syntax

The subexpression operator $() can have more than one semicolon separated statements as shown in the below-given snippet:

$(statement1; statement2; statement3; ... ; statementN)

What Does $() Operator Returns in PowerShell

As we have discussed earlier, the subexpression operator $() can take one or more statements, so the subexpression operator will return scalar for a single result and it will return an array for the multiple results.

Examples

Let’s consider some examples to understand the working of the subexpression operator $().

How to embed a String with an Expression using subexpression operator

Let’s consider the below snippet which will assist you to understand how to embed an expression with a string:

"The product of 2 * 3 = $(2*3)":

The above snippet shows that the string is embedded with the expression successfully.

Impact of Subexpression Operator

Let’s consider the below snippet to understand what will happen if we didn’t utilize the subexpression operator in the above given program:

"The product of 2 * 3 = (2*3)":

The subexpression operator first evaluates the expression and then acts on the results. However, in the above snippet we didn’t utilize the subexpression operator hence the output is a simple string.

How to embed a String with a Command using subexpression operator

Let’s consider the below example to understand how to embed a basic command with a string:

"Date & Time: $(Get-Date) ":

The snippet given above verifies the working of subexpression operator $().

Conclusion

$() is a special operator in PowerShell commonly known as a subexpression operator. It is used when we have to use one expression within some other expression. For instance, embedding the output of a command with some other expression. The subexpression operator first evaluates the expression and then acts on the results of that expression in a single line. This write-up demonstrated a comprehensive guide for the subexpression operator in PowerShell with the help of some suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.