Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
In this blog post, we’ll learn how to use the call operator.
The PowerShell call operator is used for invoking expressions that include variables, strings or script blocks as executable code. For example, you may assign an executable path to a variable and invoke it using the call operator.
$Notepad = "notepad.exe"
& $notepad
The call operator does not bring artifacts like variables, aliases or functions into the current scope when invoking PowerShell scripts. This is different than when dot sourcing scripts.
& .\myScript.ps1
You can use variables both as the executable or as parameters to the executable when using the call operator.
$Notepad = "notepad.exe"
& $notepad
$File = "powershell.ps1"
& code $File
The call operator can also be used to call script blocks. It will not bring any artifacts generated by that script block into the current scope.
& { $Variable = 123 }
$Variable
Te call operator can also be used to call scripts. Unlike dot-sourcing, it will not bring anything into the current scope.
'$Variable = 123' | Out-File .\script.ps1
& .\script.ps1
$Variable
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.