The Call Operator & in PowerShell

Migel Hewage Nimesha Feb 16, 2024
The Call Operator & in PowerShell

There are different commands in PowerShell that are unique and different from other scripting languages. This article will discuss the call operator & command or keyword used in PowerShell and its uses.

Call Operator & in PowerShell

The call operator & operator is used to achieve the required function of a command. For example, if you need to execute a certain command, you would use the & operator at the beginning of the line.

Usually, PowerShell allows running a command only if it is in the PowerShell’s environment path. Further, a command with a space is not accepted by PowerShell.

Users must type &, then start double quotation marks and include the path and command within quotes to force PowerShell to run the script. We will consider one example related to executing a script block here.

When we use the & to preserve the original value, which later changes into another value as follows.

PS C:\Users> $i = 4
PS C:\Users> $ScriptBloack = { $i = 10; echo $i }
PS C:\Users> & $ScriptBloack
PS C:\Users> $i
PS C:\Users>

Here, you can see in the script block that the value of the variable changes. If we use a command like invoke-expression, it will change, but with &, that does not happen.

The below image shows the execution and outputs.

call operator in powershell

There are many different uses of the & operator as such. You will be able to find all those in detail here and also through Microsoft documentation.

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell Operator