Category Archives: History

Invoke-History

NAME
    Invoke-History

SYNOPSIS
    Runs commands from the session history.

SYNTAX
    Invoke-History [[-Id] <string>] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Invoke-History cmdlet runs commands from the session history. You can pass objects representing the commands from Get-History to Invoke-History, or you can identify commands in the current history by using their ID number. To find the identification number of a command, use Get-History.

PARAMETERS
    -Id <string>
        Identifies a command in the history. You can type the ID number of the command or the first few characters of the command.

        If you type characters, Invoke-History matches the most recent commands first. If you omit this parameter, Invoke-History runs the last (most recent) command. The parameter name (“id”) is optional. To find the ID number of a command, use Get-History.

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

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    None
        Invoke-History does not generate any output, but output might be generated by the commands that Invoke-History runs.

NOTES

        The session history is a list of the commands entered during the session along with the ID. The session history represents the order of execution, the status, and the start and end times of the command. As you enter each command, Windows PowerShell adds it to the history so that you can reuse it. For more information about the session history, see about_History.

        You can also refer to Invoke-History by its built-in Aliases, “r” and “ihy”. For more information, see about_aliases.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Invoke-History

    Description
    ———–
    This command runs the last (most recent) command in the session history. You can abbreviate this command as “r” (think “repeat” or “rerun”), the Alias for Invoke-History.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Invoke-History -Id 132

    Description
    ———–
    This command runs the command in the session history with ID 132. Because the name of the Id parameter is optional, you can abbreviate this command as “Invoke-History 132″, “ihy 132”, or “r 132”.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Invoke-History get-pr

    Description
    ———–
    This command runs the most recent Get-Process command in the session history. When you type characters for the Id parameter, Invoke-History runs the first command that it finds that matches the pattern, beginning with the most recent commands. This command uses the ID parameter, but it omits the optional parameter name.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Invoke-History (16..24), 27

    Description
    ———–
    This command runs commands 16 through 24 and 27. You can list multiple IDs and ID ranges separated by commas.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-History -Id 255 -count 7 | Invoke-History

    Description
    ———–
    This command runs the 7 commands in the history that end with command 255 (typically 249 through 255). It uses the Get-History cmdlet to retrieve the commands. The pipeline operator (|) passes the commands to Invoke-History, which executes them.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113344
    about_History
    Get-History
    Add-History
    Clear-History

Get-History

NAME
    Get-History

SYNOPSIS
    Gets a list of the commands entered during the current session.

SYNTAX
    Get-History [[-Id] <Int64[]>] [[-Count] <int>] [<CommonParameters>]

DESCRIPTION
    The Get-History cmdlet gets the session history, that is, the list of commands entered during the current session. Windows PowerShell automatically maintains a history of each session. You can save the session history in XML or CSV format. By default, history files are saved in the home directory, but you can save the file in any location.

PARAMETERS
    -Count <int>
        Displays the specified number of the most recent history entries. The default is 32. If you use both the Count and Id parameters in a command, the display ends with the command specified by the Id parameter.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Id <Int64[]>
        Specifies the ID number of a command in the session history. Get-History gets only the specified command. If you use Id and Count, Get-History gets the most recent commands ending with the command specified by the Id parameter.

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

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    Int64
        You can pipe a history ID to Get-History.

OUTPUTS
    Microsoft.PowerShell.Commands.HistoryInfo
        Get-History returns a history object for each history item that it gets.

NOTES

        The session history is a list of the commands entered during the session along with the ID. The session history represents the order of execution, the status, and the start and end times of the command. As you enter each command, Windows PowerShell adds it to the history so that you can reuse it. For more information about the command history, see about_History.

        You can also refer to Get-History by its built-in Aliases, “h”, “history”, and “ghy”. For more information, see about_aliases.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-History

    Description
    ———–
    This command gets the 32 most recently submitted commands. The default display shows each command and its ID, which indicates the order of execution.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-History | Where-Object {$_.commandLine -like “*service*”}

    Description
    ———–
    This command gets entries from the command history that include the word, “service”. The first command gets the 32 most recent entries in the session history. The pipeline operator (|) passes the results to the Where-Object cmdlet, which selects only the commands that include “service”.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-History -Id 7 -Count 5 | Export-Csv history.csv

    Description
    ———–
    This command gets the five most recent history entries ending with entry 7. The pipeline operator (|) passes the result to the Export-Csv cmdlet, which formats the history as comma-separated text and saves it in the History.csv file. The file includes the data that is displayed when you format the history as a list, including the status and start and end times of the command.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-History -Count 1

    Description
    ———–
    This command gets the last (most recently entered) command in the command history. It uses the Count parameter to display just one command. By default, Get-History displays the most recent commands. This command can be abbreviated to “h -c 1” and is equivalent to pressing the up-arrow key.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-History -Count $MaximumHistoryCount

    Description
    ———–
    This command displays all of the commands saved in the session history. By default, $MaximumHistoryCount is 64, so this command can be abbreviated as “h -c 64”.

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-History | Format-List

    Description
    ———–
    This command displays all of the properties of entries in the session history. The pipeline operator (|) passes the result to the Format-List cmdlet, which displays all of the properties of each history entry, including the ID, status, and start and end times of the command.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113317
    about_History
    Invoke-History
    Add-History
    Clear-History

Clear-History

NAME
    Clear-History

SYNOPSIS
    Deletes entries from the command history.

SYNTAX
    Clear-History [[-Id] <Int32[]>] [[-Count] <int>] [-Newest] [-Confirm] [-WhatIf] [<CommonParameters>]

    Clear-History [[-Count] <int>] [-CommandLine <string[]>] [-Newest] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Clear-History cmdlet deletes commands from the command history, that is, the list of commands entered during the current session.

    Without parameters, Clear-History deletes all commands from the session history, but you can use the parameters of Clear-History to delete selected commands.

PARAMETERS
    -CommandLine <string[]>
        Deletes commands with the specified text strings. If you enter more than one string, Clear-History deletes commands with any of the strings.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Count <int>
        Clears the specified number of history entries, beginning with the oldest entry in the history.

        If you use the Count and Id parameters in the same command, the cmdlet clears the number of entries specified by the Count parameter, beginning with the entry specified by the Id parameter. For example, if Count is 10 and Id is 30, Clear-History clears items 21 through 30 inclusive.

        If you use the Count and CommandLine parameters in the same command, Clear-History clears the number of entries specified by the Count parameter, beginning with the entry specified by the CommandLine parameter.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Id <Int32[]>
        Deletes commands with the specified history IDs.

        To find the history ID of a command, use Get-History.

        Required?                    false
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Newest [<SwitchParameter>]
        Deletes the newest entries in the history. By default, Clear-History deletes the oldest entries in the history.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe objects to Clear-History.

OUTPUTS
    None
        This cmdlet does not generate any output.

NOTES

        The session history is a list of the commands entered during the session. You can view the history, add and delete commands, and run commands from the history. For more information, see about_History.

        Deleting a command from the history does not change the history IDs of the remaining items in the command history.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Clear-History

    Description
    ———–
    Deletes all commands from the session history.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Clear-History -Id 23, 25

    Description
    ———–
    Deletes the commands with history IDs 23 and 25.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Clear-History -command *help*, *command

    Description
    ———–
    Deletes commands that include “help” or end in “command”.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Clear-History -Count 10 -Newest

    Description
    ———–
    Deletes the 10 newest commands from the history.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Clear-History -Id 10 -Count 3

    Description
    ———–
    Deletes the three oldest commands, beginning with the entry with ID 10.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135199
    about_History
    Get-History
    Add-History
    Invoke-History

Add-History

NAME
    Add-History

SYNOPSIS
    Appends entries to the session history.

SYNTAX
    Add-History [[-InputObject] <PSObject[]>] [-Passthru] [<CommonParameters>]

DESCRIPTION
    The Add-History cmdlet adds entries to the end of the session history, that is, the list of commands entered during the current session.

    You can use the Get-History cmdlet to get the commands and pass them to Add-History, or you can export the commands to a CSV or XML file, then import the commands, and pass the imported file to Add-History. You can use this cmdlet to add specific commands to the history or to create a single history file that includes commands from more than one session.

PARAMETERS
    -InputObject <PSObject[]>
        Adds the specified HistoryInfo object to the session history. You can use this parameter to submit a HistoryInfo object from Get-History, Import-Clixml, or Import-Csv to Add-History.

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

    -Passthru [<SwitchParameter>]
        Returns a history object for each history entry. By default, this cmdlet does not generate any output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    Microsoft.PowerShell.Commands.HistoryInfo
        You can pipe a HistoryInfo object to Add-History.

OUTPUTS
    None or Microsoft.PowerShell.Commands.HistoryInfo
        When you use the PassThru parameter, Add-History generates a HistoryInfo object. Otherwise, this cmdlet does not generate any output.

NOTES

        The session history is a list of the commands entered during the session along with the ID. The session history represents the order of execution, the status, and the start and end times of the command. As you enter each command, Windows PowerShell adds it to the history so that you can reuse it. For more information about the session history, see about_History.

        To specify the commands to add to the history, use the InputObject parameter. The Add-History command accepts only HistoryInfo objects, such as those generated for each command by Get-History. You cannot pass it a path and file name or a list of commands.

        You can use the -InputObject parameter to pass a file of HistoryInfo objects to Add-History. To do so, export the results of a Get-History command to a file by using Export-Csv or Export-Clixml and then import the file by using Import-Csv or Import-Clixml. You can then pass the file of imported HistoryInfo objects to Add-History through a pipeline or in a Variable. For more information, see the examples.

        The file of HistoryInfo objects that you pass to Add-History must include the type information, column headings, and all of the properties of the HistoryInfo objects. If you intend to pass the objects back to Add-History, do not use the NoTypeInformation parameter of Export-Csv and do not delete the type information, column headings, or any fields in the file.

        To edit the session history, export the session to a CSV or XML file, edit the file, import the file, and use Add-History to append it to the current session history.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-History | Export-Csv c:\testing\history.csv

    C:\PS>Import-Csv history.csv | Add-History

    Description
    ———–
    These commands add the commands typed in one Windows PowerShell session to the history of a different Windows PowerShell session. The first command gets objects representing the commands in the history and exports them to the History.csv file. The second command is typed at the command line of a different session. It uses the Import-Csv cmdlet to import the objects in the History.csv file. The pipeline operator passes the objects to the Add-History cmdlet, which adds the objects representing the commands in the History.csv file to the current session history.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Import-Clixml c:\temp\history.xml | Add-History -Passthru | ForEach-Object {Invoke-History}

    Description
    ———–
    This command imports commands from the History.xml file, adds them to the current session history, and then executes the commands in the combined history.

    The first command uses the Import-Clixml command to import a command history that was exported to the History.xml file. The pipeline operator (|) passes the commands to the Add-History parameter, which adds the commands to the current session history. The PassThru parameter passes the objects representing the added commands down the pipeline.

    The command then uses the ForEach-Object cmdlet to apply the Invoke-History command to each of the commands in the combined history. The Invoke-History command is formatted as a script block, which is enclosed in braces ({}) because ForEach-Object requires a script block even when there is only one command to apply.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-History -id 5 -count 5 | Add-History

    Description
    ———–
    This command adds the first five commands in the history to the end of the history list. It uses the Get-History cmdlet to get the five commands ending in command 5. The pipeline operator (|) passes them to the Add-History cmdlet, which appends them to the current history. The Add-History command does not include any parameters, but Windows PowerShell associates the objects passed through the pipeline with the InputObject parameter.

    ————————– EXAMPLE 4 ————————–

    C:\PS>$a = Import-Csv c:\testing\history.csv

    C:\PS>Add-History -InputObject $a -Passthru

    Description
    ———–
    These commands add the commands in the History.csv file to the current session history. The first command uses the Import-Csv cmdlet to import the commands in the History.csv file and store its contents in the Variable $a. The second command uses the Add-History cmdlet to add the commands from History.csv to the current session history. It uses the InputObject parameter to specify the $a Variable and the PassThru parameter to generate an object to display at the command line. Without the PassThru parameter, Add-History does not generate any output to display.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Add-History -InputObject (Import-Clixml c:\temp\history01.xml)

    Description
    ———–
    This command adds the commands in the History01.xml file to the current session history. It uses the InputObject parameter to pass the results of the command in parentheses to Add-History. The command in parentheses, which is executed first, imports the History01.xml file into Windows PowerShell. Add-History then adds the commands in the file to the session history.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113279
    about_History
    Get-History
    Invoke-History
    Clear-History