152

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Leach
  • 5,517
  • 4
  • 18
  • 32

7 Answers7

308

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Leach
  • 5,517
  • 4
  • 18
  • 32
16

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

Get-Content (Get-PSReadlineOption).HistorySavePath > D:\PowerShellHistory.txt
BogdanPopa
  • 305
  • 2
  • 5
  • 4
    Getting the contents of the history file and dumping it to another file seems a bit superfluous. You could just do `copy (Get-PSReadlineOption).HistorySavePath d:\PowerShellHistory.txt` – Adam Plocher Mar 07 '22 at 20:06
11

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

Codertjay
  • 588
  • 8
  • 13
9

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

notepad (Get-PSReadlineOption).HistorySavePath
RukshanS
  • 91
  • 1
  • 2
4

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
sanigirl
  • 433
  • 3
  • 19
1

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
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Is there a way to execute a previous command from history by giving its ID? Like with bash, you enter `history`, say you want to run command 20 again, then you just do `!20`. – leo Jun 27 '22 at 21:33
  • 1
    @leo not that I know of, here's the docs, and you can look at the key bindings https://learn.microsoft.com/en-us/powershell/module/psreadline/?view=powershell-7.2 – js2010 Jun 27 '22 at 23:37
0

You can try this PowerShell Command History

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 19 '22 at 06:22