简体   繁体   中英

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.

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.

In PowerShell 3 or later, this is possible with the help of PSReadline which you can read about here .

This module does exactly what you want - it loads your history into its internal history so up/down arrows work with history from previous sessions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM