Run a PowerShell expression. Accepts a string to be executed as code. It is essential that any user input is carefully validated.

      Invoke-Expression [-command] string [CommonParameters]

   -command string
       A literal string (or variable that contains a string) that is a
       valid PowerShell expression.

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

Standard Aliases for Invoke-Expression: iex

Invoke-Expression accepts a string and treats it as PowerShell code which allows the construction of dynamic code, this means that you have to be very careful about the string input. If input is accepted from a user or any third party source, there is a possibility that they could inject unwanted additional PowerShell commands into the script with undesirable effects.

Invoke-Expression performs string expansion, so for example iex "echo Hello World $PsHome" will output

If the result of the expression is an empty array, invoke-expression will output $null

Using Invoke-Expression to run a set of commands is similar to using the & call operator but has a key difference in that invoke-expression does not create an additional scope, so any changes to variables made by the script block will remain visible.


Create variables named $sorting and $MyExpr and use them to store the text of an expression, then use invoke-expression to actually run the expression:

PS C:\> $sorting = "sort-object Name"
PS C:\> $myExpr = "get-process | $sorting"
PS C:\> invoke-expression $myExpr

“Innovation is the distinction between a leader and a follower” ~ Steve Jobs


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

Copyright © 1999-2017
Some rights reserved