Download PowerShell History

I tried to load the PowerShell story using the command

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

in my profile $.

I also wrote 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 do not see any commands appearing when I press the up arrow key. This key usually allows me to access my previous commands from the command history.

+6
source share
4 answers

I have studied this before, and it is not possible. The buffer accessed by the up arrow and function keys (for example, completion with F8 and the list that you see when you press F7 ) is executed in the session and cannot be changed.

However, what you can do to quickly access the commands in the history, including those that were added using Add-History , is the type # , followed by the template, and then press [TAB] to see all the commands in the history that correspond to pattern. For example, #dsquery[TAB] will expand to the very last command in history containing "dsquery", and pressing [TAB] will cycle through any other commands containing "dsquery" for more time.

How the pattern is matched is determined by the TabExpansion function. By default, a tab with a tab extension mainly works for strings of letters from a command, with no characters or spaces. You can check the function code by entering $function:TabExpansion . If you want, you can change the behavior of the tab extension by specifying its own TabExpansion function . If you are really not sure that you know what you are doing, I would recommend changing the existing code, and not starting from scratch, because you can violate other functions, because the TabExpansion function affects all tab completion at the prompt, for example, commands or execution paths tabs.

+12
source

Adding a bit more details:

Each PowerShell node does several things a little differently. Although PowerShell itself has the concept of a history buffer, the up / down arrows in each environment use their own internal history, not global history. Theoretically, there is no reason why Microsoft could not fix the way the history is processed on the hosts to draw attention to this (I suggest it directly). Unfortunately, the changes that could have done this would have been several years, so you are somewhat stuck at the moment.

After facing the same problems with the story, I added Icicle to IsePackV2 to visually examine the story. Just press F7 and the sidebar panel will display the real history buffer.

+3
source

What I did in the past is simply to save the history of each session in a history file with a unique name. Then I create a profile function that takes a template, and then searches all of these history files for a command line that matches and prints it. This is not as convenient as the up arrow (F7) or even Invoke-History or # get-chi. However, this also does not lead to the fact that I am loading the download history, which over time can become quite large.

+1
source

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

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

0
source

Source: https://habr.com/ru/post/950283/


All Articles