&

The call operator (&) allows you to execute a command, script or function.

Many times you can execute a command by just typing its name, but this will only run if the command is in the environment path. Also if the command (or the path) contains a space then this will fail. Surrounding a command with quotes will make PowerShell treat it as a string, so in addition to quotes, use the & call operator to force PowerShell to treat the string as a command to be executed.

Syntax
      & "[path] command" [arguments]

Key:
    command   An executable filename (.exe), script or function.

   arguments  The call operator will only handle a single command.
              Any arguments may follow the called command.

              If the pathname contains spaces, place the command
              inside double quotes and any arguments outside the quotes.

One consequence of the rules for expression parsing is that if you want to execute an EXE or script whose name starts with a number you have to quote the name and use the call operator.

Precedence of commands:

 Alias > Function > Filter > Cmdlet > Application > ExternalScript > Script
   Highest priority .................................... Lowest priority 

If you need to run a specific type of command which may not be the highest priority use Get-Command. For example if you have an external command called Ping and a function also called ping, normally the function will be run as it has higher priority, Get-Command -commandType Application Ping will return the external application instead.

Script blocks

Several commands, statements or expressions (a script block) can be stored in a variable: $myVar = { Scriptblock }
Then execute the script using &
PS C:\> & $myVar
or even without the variable:
PS C:\> & {Scriptblock}

This usage (calling a script block) is similar to using Invoke-Expression to run a set of commands but has a key difference in that the & call operator will create an additional scope, while Invoke-Expression will not.

In PowerShell 1.0 and 2.0, commands that start with a number e.g. 7z.exe will only be executed if you use the command invocation operator &

Examples

PS C:\> & "C:\Program files\mycommand.exe"

PS C:\> $prog = "C:\Program files\mycommand.exe"
PS C:\> & $prog

PS C:\> $myPing = Get-Command -commandType Application Ping.exe
PS C:\> & $myPing

PS C:\> $i = 2
PS C:\> $scriptblock = { $i=5; echo $i }
PS C:\> & $scriptblock                    # Displays 5
PS C:\> $i                                # Displays 2

PS C:\> $textblock = ' $i=5; echo $i '
PS C:\> invoke-expression $textblock      # Displays 5
PS C:\> $i                                # Displays 5

Dot-Sourcing

Invoking a command (either directly or with the call operator) will create a child scope that will be thrown away when the command exits. If the command/script changes a global variable those changes will be lost when the scope ends.
To avoid this and preserve any changes made to global variables you can 'dot' the script which will execute the script in the current scope.
PS C:\> . C:\scripts\myscript.ps1
PS C:\> . ./script64.ps1

#You went away, And I wonder where you will stay, My little runaway, Run, run, run, run, runaway# ~ Dell Shannon

Related:

Get-Command - Retrieve basic information about a command.
Invoke-Command - Run commands on local and remote computers.
Invoke-Expression - Run a PowerShell expression.
Invoke-Item - Invoke an executable or open a file (START)
Start-Process - Start one or more processes, optionally as a specific user.
--% - Stop parsing input as PowerShell commands.
. (source) - Run a command script in the current shell (persist variables and functions)
PowerShell Operators - SubExpressions Syntax
Run a PowerShell script - Syntax


© Copyright SS64.com 1999-2015
Some rights reserved