JA Control Panel

Diana Scherff [dot] com

We now have handheld and iPhone layouts!
Android users, just scan this bar code

Invoke-Expression [iex]

E-mail Print PDF
User Rating: / 0
PoorBest 

PS C:\Users\Admin> help iex -full

NAME
    Invoke-Expression

SYNOPSIS
    Runs a Windows PowerShell expression that is provided in the form of a string.

SYNTAX
    Invoke-Expression [-command] <string> [<CommonParameters>]


DETAILED DESCRIPTION
    Runs a Windows PowerShell expression that is provided in the form of a string. Invoke-Expression outputs the result of the command specified as the value of the Command parameter. However, if the result is an empty array, it outputs $null and, if the result is a single-element array, it outputs that single element.


PARAMETERS
    -command <string>
        Specifies a literal string or a variable that contains a string that is a valid Windows PowerShell expression.

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

    <CommonParameters>
        This cmdlet snpports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".


INPUT TYPE
    None


RETURN TYPE
    The output of the cmdlet is usually of the same type as the result of executing the expression represented by the string specified as the value of the Command parameter. If, however, the output of the expression is an empty array or a single-element array, then the output from the cmdlet is $null or the single element, respectively.


NOTES


        To provide a script block as the value of the Command parameter, you must include it within quotation marks. Otherwise, the shell will recognize the script block and evaluate it rather than pass it in a literal form as the value of the Command parameter. For more information about script blocks, type get-help about_script_block.

        Using the invoke-expression cmdlet, without taking the proper precautions, could compromise the security of your computing environment. This cmdlet makes it easy to write scripts that accept input that is then used to construct and run Windows PowerShell commands. The author of such a script does not have complete control over the commands the script can run. Some of the input provided to the script could represent malicious commands. Those commands will be run (under the current security context of the script) unless the script author has been careful to validate all input before using it. In general, it is best to identify and allow a set of known good input and reject all other input, rather than allow all input except that which appears to be malicious.


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

    C:\PS>$sorted_processes = "get-process | sort-object Name"
    invoke-expression $sorted_processes


    This command creates a variable named $sorted_processes and stores the text of a command in that variable. The invoke-expression cmdlet is then used to run the command stored in the sorted_processes variable.





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

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


    This command retrieves and runs an example command from cmdlet help. It runs the first example for the Get-EventLog cmdlet. To run an example for 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 you enter is not valid.

    The first line of the command stores the name of a cmdlet, get-eventlog, in the $cmdlet_name variable. The second line of the command stores the example number in the $example_number variable. In thn third line of the command, properties of the object returned by the Get-Help cmdlet are accessed to retrieve the example code and store it in the $example_code variable. In the last line of the command, the example code is run using Invoke-Expression.






RELATED LINKS
    Trace-Command
    Invoke-Item
    Invoke-History

Eats

License and Copyright

This website and its content is © Diana Scherff – 1997-2011. All rights reserved. Any redistribution or reproduction of part or all of the contents in any form is prohibited other than the following: You may print or download to a local hard disk extracts for your personal and non-commercial use only.

You may not, except with our express written permission, distribute or commercially exploit the content. Nor may you transmit it or store it in any other website or other form of electronic retrieval system.

Applies to all content unless otherwise stated.

You are here: Miscellaneous Tools Windows PowerShell Invoke-Expression