5

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?

  • 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 Nov 23 '13 at 19:53
  • Out of the box this feature does not exist. – Ramhound Nov 24 '13 at 6: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. – Shawn Melton Dec 5 '13 at 1:30
  • @ShawnMelton How do you mean? My intention was to use profiles, but I don't see how I can automatically save commands. – Louis Dec 5 '13 at 1:52
3

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.

  • 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 Dec 2 '13 at 21:41
  • I can't see an automated way to export the history on an exit. – Louis Dec 2 '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 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 2 '13 at 22:55
  • 1
    Check out 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 3 '13 at 6:10
  • 1
    Orson Tyrell has a nice implementation of Mark's suggestion: orsontyrell.blogspot.co.at/2013/11/… – Daniel Calliess Oct 22 '14 at 9:17
2

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.

  • Where is the history file? – 0fnt Jul 3 '16 at 17:13
  • @0fnt I've added the location to the answer. – Louis Jul 3 '16 at 19:44
  • 1) Isn't this only if you are using the 'PSReadline' module? 2) Is there a way to use Get-History (or '#<search term> [TAB]') against this file? – Phobis Feb 21 '17 at 21:31

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.