PowerShell Command History: HistoryInfo and PSReadLine Persistence
PowerShell has two related kinds of command history. Get-History exposes commands recorded for the current PowerShell session, while PSReadLine maintains the command lines that can be persisted and recalled in later sessions. They are connected in everyday use, but they are not the same store.
Understanding this distinction helps answer two common questions:
- Is a history entry a
HistoryInfoobject? - Does the command window write its current history to
HistorySavePathimmediately?
Session History Uses HistoryInfo
The objects returned by Get-History are normally instances of:
[Microsoft.PowerShell.Commands.HistoryInfo]
In practice, use the cmdlet or its alias rather than constructing the type yourself:
Get-History
# Alias
h
For example, filter commands that contain git:
Get-History | Where-Object CommandLine -match 'git'
Inspect the returned object type and properties with:
Get-History | Get-Member
Useful HistoryInfo properties include:
| Property | Meaning |
|---|---|
Id |
The session-specific command number. |
CommandLine |
The command text. |
ExecutionStatus |
Whether the command completed, failed, or is still running. |
StartExecutionTime |
When execution started. |
EndExecutionTime |
When execution ended. |
This is in-memory session history. Closing the session normally removes it unless it has been exported explicitly, for example with Export-Clixml.
PSReadLine Uses a Separate Persistent History
PSReadLine provides interactive editing, arrow-key history navigation, and persistent command-line history. Its configuration reveals both the history file and its saving behavior:
Get-PSReadLineOption |
Select-Object HistorySavePath, HistorySaveStyle
To read the persisted history file directly:
Get-Content (Get-PSReadLineOption).HistorySavePath
The file stores command input for future interactive sessions. It does not preserve the HistoryInfo metadata available from Get-History, such as execution status and timing.
Conceptually, the two paths are:
commands executed in this session -> Get-History -> HistoryInfo objects
commands entered at the prompt -> PSReadLine -> HistorySavePath
The overlap is common but not exact. Get-History describes commands executed in the current session; PSReadLine records interactive input history for recall.
Does PowerShell Save the Current Window Immediately?
The answer depends on PSReadLine’s HistorySaveStyle setting. Check the value first:
(Get-PSReadLineOption).HistorySaveStyle
The main options are:
| Value | Behavior |
|---|---|
SaveAtExit |
Save history when the PowerShell session exits. |
SaveIncrementally |
Append accepted command lines to the history file as they are entered. |
SaveNothing |
Do not write history to the history file. |
Set incremental saving:
Set-PSReadLineOption -HistorySaveStyle SaveIncrementally
With SaveIncrementally, PSReadLine writes history after each command executes. This is the closest behavior to real-time persistence and reduces the amount of history lost if the terminal or host process closes unexpectedly.
To inspect the most recently persisted commands:
Get-Content (Get-PSReadLineOption).HistorySavePath -Tail 20
Practical Checks
Use these commands when diagnosing what PowerShell has stored:
# Session execution history and its object type
Get-History | Select-Object -Last 10
Get-History | Get-Member
# PSReadLine persistence configuration
Get-PSReadLineOption |
Select-Object HistorySavePath, HistorySaveStyle, MaximumHistoryCount
# Inspect the commands that have already been persisted
Get-Content (Get-PSReadLineOption).HistorySavePath -Tail 20
Takeaway
[Microsoft.PowerShell.Commands.HistoryInfo] is the type normally returned by Get-History, so it represents the current session’s execution history. HistorySavePath belongs to PSReadLine and holds persisted command-line input for later sessions.
To have commands written after each execution, configure PSReadLine with SaveIncrementally. With SaveAtExit, history is persisted when the PowerShell session exits. Treat Get-History and the PSReadLine history file as complementary sources rather than interchangeable representations of the same data.