Change the order of Windows shell command history retrieval

command historycommand lineconsolepowershell

One of the things that drive me crazy in command line under MS-Windows is the order in which the Windows shell (both the standard one and PowerShell) retrieve commands from history when you use the up and down arrow keys.

The standard way I've grown to like, as it is done in basically all other shells in the world, is that the last command is put into the history stack, and then pressing up will retrieve the last command I ran, "up" again will retrieve the one from before that, and so on. If you choose a command from history and submit it again, the command is copied to the top of the stack and if you press up on the next prompt you can see that last command and then the command previous to last. If after browsing the history you press "down" past the most recent command in the history stack, you will be returned to your empty prompt (or whatever you had there before you started browsing history).

In Windows shell, it works weirdly different: if you have 3 commands in the history – for example lets call them "A", "B" and "C" – and you use the up arrow key to select command "B" and execute it again, and then use the up arrow key again on the next prompt, the shell will display "B". But this is not the last command you executed (though "B" was indeed copied to the history stack again) as if you press "up" again you will be shown "A" and not "C". If you browse down (to later entries) you will see "B" again, followed by "C" then followed by "B" again which was the last command entered – but you also can't go any further: there's no going back to an empty prompt except deleting whatever you have on the line or pressing CTRL-C.

My question – is there some setting that one can set to change this behavior?

I'm aware of the option of just getting bash or something like that and using it on Windows, but due to external constraints (as in – that's what I need to write my scripts in), I have to work in PowerShell or another Windows shell that can invoke PowerShell scripts.

Best Answer

There is a different way to control the history stack, it is not what you wanted but it is a different option you should check out.

In Properties (or Default) when you right click the top left corner there is an option "Discard Old Duplicates" tick it and the stack will change its behavior.

in your example performing "A", "B", "C". and then twice up to perform "B", the stack will look: "B","C","A" (first up will be "B")

Related Question