Siebe
Siebe

Reputation: 368

Accessing PowerShell history with up-arrow

I recently switched to powershell since my Cygwin bash started giving me senseless compilation errors when using maven. I've found how to save and restore my command history in (https://stackoverflow.com/questions/9259723/is-there-a-windows-shell-tool-can-keep-history), which seems to work (using "History" will show the recent commands after a clean start).

What I can't seem to do is access this history with the up arrow like you would if the command was used in the current session.

Any ideas?

Upvotes: 14

Views: 13045

Answers (4)

LinkPhoenix
LinkPhoenix

Reputation: 483

Like say @jhclark it is now possible

You have all the documentation for installation in the official GitHub : https://github.com/PowerShell/PSReadLine

You will have to follow the whole tutorial well and pay attention to:

  • remove -AllowPrerelease arg if you have error
  • add this line Import-Module 'PSReadLine' in your profile code $PROFILE for example to open your profile conf with vscode
  • Close all shell after you install PSReadLine if you want all work fine

Launch this two lines if you want the fleche to search in the history like on Linux because that is, I think, what most people are looking for:

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

by default are the key combinations F8 and SHIFT + F8 which are configured by default

You can see the conf with that :

Get-PSReadLineKeyHandler

UPDATE MAY 2024

PowerShell Version Windows 11 22631.3668

$PsVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.3668
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.3668
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

It has been a while since I posted that answer, and in newer versions of PowerShell, everything is now properly configured out-of-the-box. However, the answer I provided is still useful if you encounter any issues. The default values set by PSReadLine when running Get-PSReadLineKeyHandler are as follows:

UpArrow PreviousHistory
DownArrow NextHistory
...
F8 HistorySearchBackward
Shift+F8 HistorySearchForward

The last two visually do the same thing, but I believe they achieve it differently

Upvotes: 12

BartekB
BartekB

Reputation: 8650

I would suggest killing this old habit (I know, they die hard) and using PowerShell specific feature that is build for that. It's in fact pretty awesome. This is #*[tab], there are 2 options here (tab in brackets ([tab]) means that the tab button should be pressed):

#pattern_from_command[tab] 

-> Powershell cycles through all commands in history that contain "pattern_from_command".

-> EG: In Powershell: type #echo, press tab -> Powershell cycles through all commands in history that contain "echo" (of course if any).

#<id>[tab] 

-> Powershell completes command with id <id>.

-> EG: In Powershell: type #3, press tab -> Powershell writes the third (3) command in history to command line (though not executing it directly, just press enter to execute it).

I know it's not the same as you would do in bash, but I think it's worth trying and getting used to.

EDIT: It appears that recent versions of PSReadLine removed that functionality. You are better off with functionality in PSReadLine anyways, but if you want to give it a try in regular console, and it doesn't work - just

Remove-Module -Name PSReadLine

Functionality still works in any host that doesn't load PSReadLine by default (e.g. PowerShell ISE).

Upvotes: 24

jhclark
jhclark

Reputation: 2553

As of October 2013, this is now possible using the wonderful PSReadline module: http://github.com/lzybkr/PSReadLine

You'll still need to save your history when your powershell session exits and load it in your profile.ps1 prior to loading PSReadline (see http://technet.microsoft.com/en-us/library/ee156792.aspx). You can register a hook to save your history when PowerShell exists using a hook like this: Powershell profile "on exit" event?. Unlike vanilla PowerShell, PSReadLine allows the up/down keys to access this history buffer.

Upvotes: 7

Joey
Joey

Reputation: 354456

You cannot. There is no API for accessing a console program's history.

Upvotes: 1

Related Questions