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

Syntax

  Copy Code
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

none

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

none

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

<CommonParameters>

This command supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, OutBuffer, OutVariable, WarningAction, and WarningVariable. For more information, see about_CommonParameters.

Inputs and Outputs

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

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

  Copy Code
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

  Copy Code
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

  Copy Code
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

  Copy Code
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

  Copy Code
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

  Copy Code
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.

See Also