Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Get-History

Updated: October 17, 2013

Applies To: Windows PowerShell 4.0

Get-History

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

Aliases

The following abbreviations are aliases for this cmdlet:

  • ghy, h, history

Syntax

Parameter Set: Default
Get-History [[-Id] <Int64[]> ] [[-Count] <Int32> ] [ <CommonParameters>]




Detailed 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. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable. Beginning in Windows PowerShell 3.0, the default value is 4096.

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.

For more information about the history features in Windows PowerShell, see about_History (http://go.microsoft.com/fwlink/?LinkID=113233).

Parameters

-Count<Int32>

Displays the specified number of the most recent history entries. By, default, Get-History gets all entries in the session history. If you use both the Count and Id parameters in a command, the display ends with the command that is specified by the Id parameter.

In Windows PowerShell 2.0, by default, Get-History gets the 32 most recent entries.


Aliases

none

Required?

false

Position?

2

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Id<Int64[]>

Specifies the ID number of an entry in the session history. Get-History gets only the specified entry. If you use both the Id and Count parameters in a command, Get-History gets the most recent entries ending with the entry specified by the Id parameter.


Aliases

none

Required?

false

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

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

  • Int64

    You can pipe a history ID to Get-History.


Outputs

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

  • 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. 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 (http://go.microsoft.com/fwlink/?LinkID=113233).

  • Beginning in Windows PowerShell 3.0, the default value of the $MaximumHistoryCount preference variable is 4096. In Windows PowerShell 2.0, the default value is 64. For more information about the $MaximumHistoryCount variable, see about_Preference_Variables (http://go.microsoft.com/fwlink/?LinkID=113248).

Examples

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

This command gets the entries in the session history. The default display shows each command and its ID, which indicates the order of execution.


PS C:\> Get-History

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

This command gets entries in the command history that include "service". The first command gets all entries in the session history. The pipeline operator (|) passes the results to the Where-Object cmdlet, which selects only the commands that include "service".


PS C:\> Get-History | Where-Object {$_.CommandLine -like "*Service*"}

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

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.


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

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

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 gets the most recent commands. This command can be abbreviated to "h -c 1" and is equivalent to pressing the up-arrow key.


PS C:\> Get-History -Count 1

-------------------------- EXAMPLE 5 --------------------------

This command displays all of the properties of entries in the session history. The pipeline operator (|) passes the results of a Get-History command 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.


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

Related topics



Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.