Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading PowerShell history

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

Import-Clixml ~\history.clixml | Add-History

in my $profile.

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

function global:xx 
{
  Get-History | Export-Clixml ~\history.clixml
  exit
}

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.

like image 454
Eric Avatar asked Jul 25 '13 15:07

Eric


2 Answers

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.

like image 88
Adi Inbar Avatar answered Sep 20 '22 02:09

Adi Inbar


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.

like image 40
Start-Automating Avatar answered Sep 20 '22 02:09

Start-Automating