Loading PowerShell history Loading PowerShell history powershell powershell

Loading PowerShell history


I've looked into this before, and it's not possible. The buffer accessed with the up arrow and function keys (such as completion with F8 and the list you see when you hit F7) is per-session and cannot be modified.

However, to quickly access commands in the history, including ones that were added with Add-History, you can type # followed by a pattern, then hit [TAB] to cycle through all commands in the history that match the pattern. For example, #dsquery[TAB] will expand to the most recent command in the history containing "dsquery", and hitting [TAB] more times will cycle backwards through any other commands that contain "dsquery".

How the pattern is matched is determined by the TabExpansion function. By default, tab-expanding history entries mostly works well for strings of letters from the command, no symbols or spaces. You can examine the function's code by entering $function:TabExpansion. If you want, you can modify the behavior of tab expansion by defining your own TabExpansion function. Unless you're really sure you know what you're doing, though, I'd recommend tweaking the existing code rather than starting from scratch, because you can break other functionality, because the TabExpansion function affects all tab completion at the prompt, such as tab-completing commands or paths.


Adding a bit more detail:

Each PowerShell host does a few things slightly differently. While PowerShell itself has a concept of a history buffer, the up/down arrows in each of the environments use their own internal history, rather than the global history. Theoretically, there's no reason why Microsoft couldn't fix the way history is handled in the hosts to pay attention to it (I will suggest it directly). Unfortunately, changes that would do that would be several years off, so you're somewhat stuck for now.

Having faced the same pains with history, I added an Icicle to IsePackV2 to visually explore history. Just press F7 and the sidebar pane will show the real history buffer.


In the past I have just saved the history of each session to a uniquely-named history file. Then I created a profile function that took a pattern and then searched through all those history files for a command line that matched and printed it out.

It's not as convenient as up arrow (F7) or even Invoke-History or #get-chi. However it also doesn't bog down my startup, loading history that can grow quite large over time.