Powershell

Call operator “&” in PowerShell

There are a number of operators including assignment, comparison, arithmetic, etc in Powershell. These operators have their own purposes and usages. The call “&” operator is also one of them. The call “&” operator can be defined as the operator that invokes the command, script file, function, and operable programs in PowerShell. Because of the invoking behavior of the call operator, it is also called the “Invocation operator”.

This article illustrates PowerShell’s primary usage of the call “&” operator.

How to Use the Call operator “&” in PowerShell?

The call operator is a PowerShell-supported operator used for calling or invoking the PowerShell command written in script blocks or strings. Remember, do not mix the call operator with the background operator, because both have the same ampersand “&” sign. If the ampersand “&” is mentioned at the start, it will invoke or call the command. But, If the ampersand “&” is placed at the end of the command it will work as a background operator.

The given-below examples will explain the primary purpose of the call operator.

Example 1: How to call a variable using the call “&” operator?

To understand the use of the call “&” operator, we need some sample scripts to practice. In the below example, we have stored the Get-Date command in a variable $date. But the important thing is that the Get-Date command is here, considered as a string value.

$date = "Get-Date"

Now, call the $date variable to run the script, as shown below.

$date

It can be noticed that the $date did not run properly. It just returned the value stored as a string.

The $date is executed with the call “&” operator as below-mentioned syntax.

& $date

As you can see the $date is run with the help of the call “&” operator.

While using the call “&” operator, the command parameter does not work inside the double-quotes. The below-mentioned example will explain the primary use of the call “&” operator

$date = "{Get-Date -DisplayHint Date}"

In the above code, we have assigned the Get-Date command to the $date variable. But this time, we also added the Get-Date parameter “-DisplayHint” within a string.

Execute the value stored in the script file with “& $date” as mentioned below.

& $date

It can be observed from the above message, that the command parameter is not recognized as a command, function, script, or operable program file. Thus, we cannot run the call operator on command parameters.

Example 2: How to use the Call Operator to User Input Script?

In the following script we are going to get some input from a user and then display the output with help of the call operator.

$call = {$age = Read-Host "Enter your age"
                Write-Host "You are $age years old"
        }

In the above script we used Read-Host to prompt from the user input and the Write-Host is used to display the output. This two-liner code is now stored in the $call variable.

The $call is run without using the call operator as shown below.

$call

It can be seen that the $call prints the whole script as it was written in the script file.

Let’s examine the output of $call when it was called with the “&” operator.

& $call

As per the above-mentioned output, it can be observed that the script file executed and displayed the results based on user input.

Example 3: How to Measure/Count Objects using Call “&” Operator?

The Measure-Object or Measure command is used in PowerShell to calculate or count objects, files, text, etc. In the following example, we want to count a few numbers with the help of the Measure-Object command but at the same time, we stored the code in the $Mr_obj variable. With variables such as $Mr_obj, we don’t have to write code again and again. Here the curly braces mean that the commands in braces will not execute until it is called using the call “&” operator.

$Mr_obj = { echo 1,2,3,4,5 | Measure-Object }

Now, call the command presented in curly braces “{ }” using the “& $Mr_obj”.

& $Mr_obj

After the execution of the above-mentioned command, you will find the output in your console.

Example 4: How to Invoke the Script file with absolute path?

First, we need to write a simple script to print the greeting words in the console. The sample code is written below.

$greetings = Write-Host "Hello PowerShell!" -ForegroundColor Green

But, when we called the call operator with the absolute path of the script file, as given below.

& "C:\Users\powershell\Documents\greetings.ps1"

It can be seen in the above results, it displayed the message which was written inside the script body. Great work! You have learned about the use of the PowerShell call operator in this article.

Conclusion

The call “&” operator can be used in Powershell to invoke the variables written in specific script files. The commands, functions, and operable programs can be called while using the call “&” operator. We learned about the various uses of the call “&” operator in this post. We have also discussed the main difference between the call operator and the background operator in PowerShell.

About the author

Adnan Shabbir