Invoke-Expression

Run a PowerShell expression.

Syntax
      Invoke-Expression [-command] string [CommonParameters]

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

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable. 

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

Example

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"
$MyExpr = "get-process | $sorting"
invoke-expression $MyExpr


"Innovation is the distinction between a leader and a follower" - Steve Jobs

Related Powershell Commands:

Trace-Command -Trace an expression or command
Invoke-Item - Invoke an executable or open a file
Invoke-History - Invoke a previously executed Cmdlet



Back to the Top

Simon Sheppard
SS64.com