Runs commands or expressions on the local computer. (Invoke-Expression)



Runs commands or expressions on the local computer.

Invoke-Expression [-Command] <string> [<CommonParameters>]

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.

-Command <string>
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.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false

This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, type,
"get-help about_commonparameters".

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.

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.


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

C:\PS> $command

C:\PS> 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

# Description
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.


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

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

# Description
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.


C:\PS>$cmd = 'get-process | where {$_.cpu -gt 1000}'

C:\PS> iex $command

# Description
This example runs a command string that is saved in the $cmd 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 string.


C:\PS>$cmdlet_name = "get-eventlog"

C:\PS> $example_number = 1

C:\PS> $example_code = (get-help $cmdlet_name).examples.example[($example_number-1)].code

C:\PS> invoke-expression $example_code

# Description
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.

Online version:

C:\Windows>powershell get-help Get-Alias -full

ColorConsole [Version 1.7.1000] PowerShell 2.0-Export
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2014 Microsoft Corporation.

OS: Windows-10 / Windows-8.1 & 8 / Windows-7 & Vista / Windows Server 2008-2016
»»»» ColorConsole


... Home  
... Impressum
... ColorConsole

... CMD
... Netsh-XP
... Netsh-7
... Netsh-Vista
... Powershell

Download the Windows 7 Starter, Home (Basic Premium), Professional und Ultimate!

Kann ich die Symbole in Gruppen anzeigen, bzw. anordnen?  / Programmfenster maximieren um mehr Infos zu sehen?  / Kalibrierung der Bildschirmfarben unter Windows-10 bei Laptops und PCs?  / Wie kann ich in der 'Windows-7 Startmenü Suche' ohne die Eingabe von vielen Zeichen suchen?  / Was ist TMC bei einem Autoradio?  / Wie kann ich den Favoritenordner ändern?  / Gibt es eine portable Freeware, um gelöschte Dateien wiederherzustellen?  / Wie kann ich das Administrator-Konto von Windows 8.1 und Win 10 aktivieren?  / Is the Full Screen function in the Magnifier under Windows XP or only in Windows 7 possible?  / What does restore the clipboard?  / Disable AutoPlay for all users in Windows 8.1, 8 and 10! (CD, DVD, USB)  / Wie kann ich in Win-7 die Benutzerkontensteuerung einstellen, bzw. die Benachrichtigungshäufigkeit bei Programmen?  /