liveBook · Manning

Invocation operator

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

The invocation operator is the PowerShell ampersand operator that executes a command whose name or path is supplied as an expression.

The invocation operator, written &, runs a command whose executable name or path is stored in an expression. It is useful when PowerShell needs to run an external utility with values held in variables. section 4.7

Run an external utility from variables

$exe = "func" $action = "new" $language = "powershell" $template = "HttpTrigger" $name = "myFunc" & $exe $action -l $language -t $template -n $name PowerShell passes the executable, action, parameters, and values as separate command elements.

Use the operator when:

  • The executable path or command name is supplied through a variable. section 4.7
  • An external command is difficult for PowerShell to parse directly. section 4.7
  • For PowerShell v3 and later, the --% stop-parsing symbol can pass literal arguments instead, but variables will not be expanded. section 4.7
$exe = "func"
$action = "new"
$language = "powershell"
$template = "HttpTrigger"
$name = "myFunc"
& $exe $action -l $language -t $template -n $name

Browse the complete runnable pattern for invoking an external utility with command and parameter values stored in variables. section 4.7

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Demonstrates a complete variable-based command pattern and contrasts it with PowerShell's later stop-parsing syntax.
Examples
Uses the func utility to create an Azure Functions HTTP trigger from variable-held action, language, template, and name values.
What you can do after
Apply it via PowerShell to run external utilities with dynamically supplied executable names and argument values.
How it connects
Connects command execution with variables, parameters, argument parsing, and external command-line utilities.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.