Run a PowerShell expression.

      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

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

Invoke-Expression can be used to perform string expansion but if the string is supplied by an end-user you need to ensure that it does not contain malicious code/expressions.


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


Run/Call - Run a command (call operator)
Trace-Command -Trace an expression or command
Invoke-Item - Invoke an executable or open a file
Invoke-History - Invoke a previously executed Cmdlet

© Copyright 1999-2015
Some rights reserved