Get-History | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Clear-History. 

When to use Clear-History?

The Get-History cmdlet gets the session history, that is, the list of commands entered during the current session.

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. By default, history files are saved in the home directory, but you can save the file in any location.

The session history is managed separately from the history maintained by the PSReadLine module. Both histories are available in sessions where PSReadLine is loaded.

This cmdlet only works with the session history.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-History?

Get the session history:

Get-History

The Get-History cmdlet gets the entries in the session history. The default display shows each command and its ID, which indicates the order in which they ran.

Get entries that include a string:

1st command: Get-History | Where-Object {$_.CommandLine -like “*Service*”}

2nd command: Get-History | Where-Object {$_.CommandLine -like “*Help*”}

The first command gets entries in the command history that include the string 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.

The second command gets entries in the command history that include the string help.
The second 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.

 

Display the most recent command:

Get-History -Count 1

This gets the last command in the command history. The last command is the most recently entered command. This command 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.

 

 

Display all the properties of the entries in the history:

Get-History | Format-List -Property *

This 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. This includes the ID, status, and start and end times of the command.

Learn last week’s command Add-History here.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *