PowerShell Logo Small

Invoke-Expression



This is the built-in help made by Microsoft for the command 'Invoke-Expression', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Runs commands or expressions on the local computer.

SYNTAX


Invoke-Expression [-Command] <String> [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]]
[<CommonParameters>]



Search powershellhelp.space

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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293986
Invoke-Command

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>$command = "Get-Process"
PS C:\>$command
Get-Process

PS C:\>invoke-expression $command

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
296 4 1572 1956 20 0.53 1348 AdtAgent
270 6 1328 800 34 0.06 2396 alg
67 2 620 484 20 0.22 716 ati2evxx
1060 15 12904 11840 74 11.48 892 CcmExec
1400 33 25280 37544 223 38.44 2564 communicator
...



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.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>invoke-expression -command "C:\ps-test\testscript.ps1"
PS C:\>"C:\ps-test\testscript.ps1" | invoke-expression



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.










-------------------------- EXAMPLE 3 --------------------------

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



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.










-------------------------- EXAMPLE 4 --------------------------

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



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.