How can I see the command history across all PowerShell sessions in Windows Server 2016?

PowershellWindows Server-2016

Powershell Problem Overview


Where can you view the full history from all sessions in Windows Server 2016?

The following PowerShell command only includes the commands from the current session:

Get-History

Powershell Solutions


Solution 1 - Powershell

In PowerShell enter the following command:

(Get-PSReadlineOption).HistorySavePath

This gives you the path where all of the history is saved. Then open the path in a text editor.

Try cat (Get-PSReadlineOption).HistorySavePath to list the history in PowerShell.

Solution 2 - Powershell

For getting full history from PowerShell and save the output to file I use this command:

Get-Content (Get-PSReadlineOption).HistorySavePath > D:\PowerShellHistory.txt

Solution 3 - Powershell

Since you are on windows you can also use below to open 'notepad' with it.

notepad (Get-PSReadlineOption).HistorySavePath

Solution 4 - Powershell

The Psreadline module 2.1 beta1 on Powershell gallery (Powershell 7 only) https://www.powershellgallery.com/packages/PSReadLine/2.1.0-beta1 does intellisense on the commandline using the saved history: https://github.com/PowerShell/PSReadLine/issues/1468 It's been starting to show up in Vscode. https://www.reddit.com/r/PowerShell/comments/g33503/completion_on_history_in_vscode/

Also in Psreadline, you can search the saved history backwards with either f8 (after typing something on the command line) or control-R. Get-psreadlinekeyhandler lists the key bindings.

get-psreadlinekeyhandler -bound -unbound | ? function -match history

Solution 5 - Powershell

There's mention of Windows Server/Enterprise editions, but as a Pro (standard retail version) user HistorySavePath is also available to me. I needed to see what python packages were recently installed in an older session and wanted to add an answer here for people looking for specific things in the history.

# if you like file names and line numbers included in your output
Select-String "<search pattern>" (Get-PSReadlineOption).HistorySavePath

# if you Just want the text without any of the other information
Get-Content (Get-PSReadlineOption).HistorySavePath | Select-String "<search pattern>" 

In my case I ran

Select-String 'pip install' (Get-PSReadlineOption).HistorySavePath

which gave me a list of pip install commands run from my previous sessions

...
[Path/To/File]:10401:pip install dash
[Path/To/File]:10824:pip install -r .\requirements.txt
[Path/To/File]:11296:pip install flask-mysqldb
[Path/To/File]:11480:pip install Flask-Markdown
[Path/To/File]:11486:pip install pygments
[Path/To/File]:11487:pip install py-gfm
[Path/To/File]:11540:pip install bs4

Solution 6 - Powershell

You can try this PowerShell Command History

Solution 7 - Powershell

On windows PowerShell

To get in a session you can use h or history

but to get all commands written in the computer you use

cat (Get-PSReadlineOption).HistorySavePath

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel LeachView Question on Stackoverflow
Solution 1 - PowershellDaniel LeachView Answer on Stackoverflow
Solution 2 - PowershellBogdan PopaView Answer on Stackoverflow
Solution 3 - PowershellRukshanSView Answer on Stackoverflow
Solution 4 - Powershelljs2010View Answer on Stackoverflow
Solution 5 - PowershellsaniboyView Answer on Stackoverflow
Solution 6 - PowershellMaciej TrudnosView Answer on Stackoverflow
Solution 7 - PowershellFavour AfenikhenaView Answer on Stackoverflow