Get-History

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

Syntax
      Get-History [ [-count] int ]
        [ [-id] Int64[] ]
          [CommonParameters]

Key
   -count 
       Display n of most-recent history entries.
       The default is 32. 

   -id Int64
       The ID number of a command in the session history.
       Get-History gets only the specified command.

Standard Aliases for Get-History: h, history, ghy

If you specify both -Count and -Id, the display will end with the command specified by Id.

Keyboard shortcuts for PowerShell History:

Pressing the Up-arrow key will move backwards through the command history, this is equivalent to get-history -count 1 which can be abbreviated to to "h -c 1"

Pressing the Down-arrow key will move forwards through the command history, starting with the first command (the oldest in the history buffer)

Press F7 to display the command history.
With the history displayed you can either use the Up/Down arrow keys and then [Return] to run the command, or press F9, type the number assigned to the command and then [Return] to run the command.

Press F8 to search history.
Type one or more characters and then press F8 to display the last command entered that begins with those characters. press F8 again to displays the next matching command, and so on.

By default, PowerShell will store the most recent 64 commands in the command history, this can be increased by setting the variable $MaximumHistoryCount. For example, $MaximumHistoryCount = 100

Insert key - Toggle Insert/Overwrite
Ctrl+Left Arrow - move the cursor one word to the left
Ctrl+Right Arrow - move the cursor one word to the right
Home/End - Move to the start/end of the current command line.
Tab key - Automatically complete a path

Examples

Get the (32) most recent entries from the session history, and pipe the results to the Where-Object to select only commands that include "service":

PS C:\> get-history | where-object {$_.commandLine -like "*service*"}

Display the properties of entries in the session history:

PS C:\> get-history | format-list

Get the 50 most recent entries from the session history and pipe the result to to Export-Csv, saving it as a comma-separated text file:

PS C:\> get-history -count 50 | export-csv history.csv

“History will be kind to me for I intend to write it” ~ Winston Churchill

Related PowerShell Cmdlets

$$ - the last item on the last line you typed (like !$ in bash).
Add-History - Add entries to the session history.
Clear-History - Delete entries from the command history.
Invoke-History - Invoke a previously executed command.
Windows Keyboard shortcuts
Preserve Command History Across Sessions


 
Copyright © 1999-2024 SS64.com
Some rights reserved