Updated: August 9, 2015


Runs commands or expressions on the local computer.


The following abbreviations are aliases for this cmdlet: 

  • iex


Parameter Set: Default
Invoke-Expression [-Command] <String> [ <CommonParameters>]

Detailed Description

The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line would be returned (echoed) unchanged.



Specifies the command or expression to run. Type the command or expression or enter a variable that contains the command or expression. The Command parameter is required.







Default Value


Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?



This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.


The input type is the type of the objects that you can pipe to the cmdlet.

  • System.String or PSObject

    You can pipe an object that represents the command to Invoke-Expression. Use the $Input automatic variable to represent the input objects in the command.


The output type is the type of the objects that the cmdlet emits.

  • PSObject

    Returns the output that is generated by the invoked command (the value of the Command parameter).


  • An expression is a statement that can be evaluated and produces a result, such as a Windows PowerShell command.

  • Take reasonable precautions when using the Invoke-Expression cmdlet in scripts. When using Invoke-Expression to run a command that the user enters, verify that the command is safe to run before running it. In general, it is best to design your script with predefined input options, rather than allowing freeform input.


Example 1: Evaluate an expression

This example demonstrates the use of Invoke-Expression to evaluate an expression. Without Invoke-Expression, the expression is printed, but not evaluated.

The first command assigns a value of Get-Process (a string) to the $Command variable.

The second command shows the effect of typing the variable name at the command line. Windows PowerShell echoes the string.

The third command uses Invoke-Expression to evaluate the string.

PS C:\> $Command = "Get-Process"
PS C:\>$Command
PS C:\> Invoke-Expression $Command

Example 2: Run a script on the local computer

These commands use Invoke-Expression to run a script, TestScript.ps1, on the local computer. The two commands are equivalent. The first uses the Command parameter to specify the command to run. The second uses a pipeline operator (|) to send the command string to Invoke-Expression.

PS C:\> Invoke-Expression -Command "C:\ps-test\testscript.ps1"
PS C:\>"C:\ps-test\testscript.ps1" | Invoke-Expression

Example 3: Run a command in a variable

This example runs a command string that is saved in the $Command variable.

The command string is enclosed in single quotation marks because it includes a variable, $_, which represents the current object. If it were enclosed in double quotation marks, the $_ variable would be replaced by its value before it was saved in the $Command variable.

PS C:\> $Command = 'Get-Process | where {$_.cpu -gt 1000}'
PS C:\>Invoke-Expression $Command

Example 4: Get and run a cmdlet Help example

This command retrieves and runs the first example in the Get-EventLog cmdlet Help topic.

To run an example of a different cmdlet, change the value of the $Cmdlet_name variable to the name of the cmdlet. And, change the $Example_number variable to the example number you want to run. The command will fail if the example number is not valid.

PS C:\> $Cmdlet_name = "Get-EventLog"
PS C:\>$Example_number = 1
PS C:\>$Example_code = (Get-Help $Cmdlet_name).examples.example[($Example_number-1)].code
PS C:\>Invoke-Expression $Example_code

Related topics

Community Additions