How to See the Command History Across All PowerShell Sessions

Rohan Timalsina Feb 02, 2024
How to See the Command History Across All PowerShell Sessions

PowerShell maintains a history of each session. PowerShell’s Get-History cmdlet lists all commands entered during the current session.

The $MaximumHistoryCount variable determines the number of entries in the session history. The default value is 4096.

It shows each command and its ID, which indicates the order in which they are executed.

Get-History

Output:

  Id CommandLine
  -- -----------
   1 Get-Date
   2 Get-ChildItem
   3 Get-Content test.txt
   4 Get-Command gcc

The Get-History displays the previous commands entered in the current session only.

By default, PowerShell stores the command history of all sessions in a text file located in a user’s home directory. This tutorial will teach you to see the command history across all PowerShell sessions.

Use the (Get-PSReadlineOption).HistorySavePath to See the Command History Across All PowerShell Sessions

The following command prints the path of a file where the command history is saved.

(Get-PSReadLineOption).HistorySavePath

Output:

C:\Users\rhntm\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

You can use the Get-Content cmdlet to list all the contents saved in that file.

Get-Content (Get-PSReadLineOption).HistorySavePath

OR

Get-Content C:\Users\rhntm\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt

Both commands will print the long list of commands on the console executed previously across all PowerShell sessions.

Here is a short overview of the output.

Output:

Get-Date
date = Get-Date -format "yyyyMMdd"
$date = Get-Date -format "yyyyMMdd"
date
$date
$dateStr = $date -format "yyyyMMdd"
$dateStr = date -format "yyyyMMdd"
$dateStr

You can also open the file in text editors like notepad and view the command history.

notepad (Get-PSReadLineOption).HistorySavePath

Output:

see the command history across all powershell sessions

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website