Loading PowerShell history

Question:

I’ve tried loading my PowerShell history by using the command

in my $profile.

I’ve also written a custom exit function that saves them:

I type “xx” to exit PowerShell, and then restart PowerShell. Although it loads my history.clixml without any errors, I don’t see any commands show up when I click the up arrow key. This key normally works to let me access my previous commands from my command history.

Answer:

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.

Source:

Loading PowerShell history by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply