6

Is there a way to have PowerShell remember commands entered during the previous sessions? That is, after closing the window and opening it again, can PowerShell be setup to remember the commands?

Louis Waweru
  • 23,945
  • 39
  • 132
  • 198
  • A Powershell command window does not keep a log by default. You should have to use a third-party command shell to get this functionality. – Ramhound Nov 23 '13 at 19:50
  • @Ramhound Darn, I was hoping it could take a history file, like Bash does. – Louis Waweru Nov 23 '13 at 19:53
  • Out of the box this feature does not exist. – Ramhound Nov 24 '13 at 06:27
  • If you need it to recall commands you use over and over why not use profiles? You can also look at using Start-Transcript. –  Dec 05 '13 at 01:30
  • @ShawnMelton How do you mean? My intention was to use profiles, but I don't see how I can automatically save commands. – Louis Waweru Dec 05 '13 at 01:52
  • Why would you automatically need to save them? Do you literally need to save the exact command, or the purpose of the command? If I issue a command that I find myself using a second time then I out that command in "my notebook" or profile for use again. –  Dec 05 '13 at 02:43
  • http://blogs.msdn.com/b/powershell/archive/2006/07/01/perserving-command-history-across-sessions.aspx came across this from the architect behind Powershell. You would just need to train yourself around not tying exit or hitting the red x to close the window. –  Dec 05 '13 at 03:22

2 Answers2

3

This answer applies only to older PowerShell versions. At least since ver. 5 PowerShell persists history to a file automatically

With a little bit of scripting you could set this up, even without third party software. I would recommend reading the help on Get-History, Add-History, and about_History.

As the help explains, you can use Get-History to get your current history and with a command such as Get-History | Export-CliXml C:\History.CliXml save it to a file. Following that, you could import your history using the Add-History command.

(The Add-History help file actually explains how to do this in more detail.)

I would expect that it is possible to configure your environment to perform these actions automatically, although I would question the usefulness of such a setup.

ndemou
  • 1,050
  • 1
  • 10
  • 18
Mark
  • 94
  • 2
  • 1
    +1, thanks. The functionality is all there. But this would be a bit too cumbersome. Just pressing the up arrow without having to use Add-History would be ideal. – Louis Waweru Dec 02 '13 at 21:41
  • I can't see an automated way to export the history on an exit. – Louis Waweru Dec 02 '13 at 21:47
  • I see what you mean. I can't find a way to make the arrow keys work with the imported history (not sure why), but you might be able to use something from [this](https://lopsa.org/content/persistent-history-powershell) to help with automation. Unfortunately it seems that it's extremely difficult to make any code execute when the x button is clicked; you have to actually type exit. – Mark Dec 02 '13 at 22:55
  • 1
    Check out http://www.github.com/lzybkr/PSReadline - it supports arrow keys with imported history. V2 had a bug so that clicking on the x did not reliably run your exit handler, that bug was fixed in V3. – Jason Shirk Dec 03 '13 at 06:10
  • 1
    Orson Tyrell has a nice implementation of Mark's suggestion: http://orsontyrell.blogspot.co.at/2013/11/true-powershell-command-history.html – Daniel Calliess Oct 22 '14 at 09:17
3

The Windows PowerShell in Windows 10 has this functionality by default.

The history file is located at AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt.

Louis Waweru
  • 23,945
  • 39
  • 132
  • 198