Categories
Interior

How to use your command history in windows powershell

Chris Hoffman is Editor-in-Chief of How-To Geek. He’s written about technology for over a decade and was a PCWorld columnist for two years. Chris has written for The New York Times, been interviewed as a technology expert on TV stations like Miami’s NBC 6, and had his work covered by news outlets like the BBC. Since 2011, Chris has written over 2,000 articles that have been read nearly one billion times—and that’s just here at How-To Geek. Read more.

How to use your command history in windows powershell

Windows PowerShell has a built-in command history feature that provides detailed information about the commands you’ve run. Like the Command Prompt, PowerShell only remembers your command history for the current session.

How to Use the Command-Line Buffer

PowerShell technically has two types of command history. First, there’s the commandline buffer, which is actually part of the graphical PowerShell terminal application and not part of the underlying Windows PowerShell application. It provides a few basic features:

  • Up Arrow: Recall the previous command you typed. Press the key repeatedly to walk through your command history.
  • Down Arrow: Recall the next command you typed. Press the key repeatedly to walk through your command history.
  • F8: Search your command history for a command matching the text on the current command line. So, if you wanted to search for a command that began with “p”, you’d type “p” on the command line and then repeatedly tap F8 to cycle through commands in your history that begin with “a”.

By default, the buffer remembers the last 50 commands you typed. To change this, right-click the title bar of the PowerShell prompt window, select “Properties”, and change the value of “Buffer Size” under Command History.

How to use your command history in windows powershell

How to View PowerShell History

Windows PowerShell itself keeps a history of the commands you’ve typed in the current PowerShell session. You can use several included cmdlets to view and work with your history.

To view the history of commands you’ve typed, run the following cmdlet:

How to use your command history in windows powershell

You can search your history by piping the resulting output to the Select-String cmdlet and specifying the text you want to search for. Replace “Example” in the cmdlet below with the text you want to search for:

How to use your command history in windows powershell

To view a more detailed command history that displays the execution status of each command along with its start and end times, run the following command:

How to use your command history in windows powershell

By default, the Get-History cmdlet only shows the 32 most recent history entries. If you want to view or search a larger number of history entries, use the -Count option to specify how many history entries PowerShell should show, like so:

How to use your command history in windows powershell

How to Run Commands From Your History

To run a command from your history, use the following cmdlet, specifying the Id number of the history item as shown by the Get-History cmdlet:

To run two commands from your history back to back, use Invoke-History twice on the same line, separated by a semicolon. For example, to quickly run the first command in your history and then the second, you’d run:

How to use your command history in windows powershell

How to Clear Your PowerShell History

To clear the history of commands you’ve typed, run the following cmdlet:

Note that the command line buffer is separate from the PowerShell history. So, even after you run Clear-History , you can continue to press the up and down arrow keys to scroll through commands you’ve typed. However, if you run Get-History , you’ll see that your PowerShell history is in fact empty.

PowerShell doesn’t remember your history between sessions. To erase both command histories for the current session, all you have to do is close the PowerShell window.

If you’d like to clear the PowerShell window after clearing the history, you can do it by running the Clear command:

How to Save and Import Your PowerShell History

If you want to save the PowerShell command history for the current session so you can refer to it later, you can do so.

This exports your command history as a detailed XML file complete with “StartExecutionTime” and “EndExecutionTime” values for each command that tell you when the command was run and how long it took to complete.

How to use your command history in windows powershell

Once you’ve exported your PowerShell history to such an XML file, you (or anyone else you send the XML file to) can import it to another PowerShell session with the Add-History cmdlet:

If you run the Get-History cmdlet after importing such an XML file, you’ll see that the commands from the XML file were imported into your current PowerShell session’s history.

By Sarah | Follow | Last Updated April 07, 2020

Summary :

How to use your command history in windows powershell

Actually, the command history is a feature built in many operating system shells, like Windows PowerShell commands. By using this feature, users can recall, edit and rerun the commands executed previously. In this post, I will show you how to check and use the command history easily on Windows.

What Is Command History?

In 1978, the command line history was firstly introduced to Unix in Bill Joy’s C shell. After that, command history became a standard feature in common shells. The history feature remembers all the commands users have executed before so it can help them find and reuse the previous commands easily. (If you need to backup data or manage disks, you should resort to MiniTool Solution.)

How to use your command history in windows powershell

Windows PowerShell, the task automation and configuration management framework, also contains the history feature. But what you need to remember is that the PowerShell history is only available for the current session (this is similar to Command Prompt).

How do you use Command Prompt as a data recovery tool?

How to use your command history in windows powershell

This page will show you how to recover files using CMD easily. You can recover data from USB pen drive, hard disk or other storage devices by command line.

How to See PowerShell History

First of all, I’d like to show you how to open Windows PowerShell.

  1. Right click on the Windows button in the lower left corner.
  2. Choose Windows PowerShell or Windows PowerShell (Admin) from the WinX menu.
  3. Now, you’ll be brought to the Windows PowerShell window directly.

How to use your command history in windows powershell

After that, I will show you different ways to see the PowerShell commands you have used in the same session by using keyboard.

* 1. F8 Button

This method works under the condition that you can remember the certain command partially.

  1. Type the part of the command that you can recall.
  2. Press F8 on the keyboard.
  3. Keep pressing the button until you find the command you need.

How to use your command history in windows powershell

This method can help you search the PowerShell commands for the one that matches the text you type. For instance, if you want to search for a common starting with the letter “s”, you should type s and then press F8 repeatedly on the keyboard to look through the commands in your history that begin with “s”.

* 2. Ctrl + R and Ctrl + S Shortcuts

If you’d like to search forward or backward in command history while using PowerShell, you should make use of the keyboard shortcuts.

  • Ctrl + R: this shortcut key combination is used to help you search backward among the PowerShell command history. In addition, you can type the Up arrow button on the keyboard to recall the previous command.
  • Ctrl + S: this shortcut key combination is used to help you search forward among the PowerShell command history. And you can also type the Down arrow button to recall the next command you have typed.

How to use your command history in windows powershell

* 3. Get-History

You can type Get-History into an opening Windows PowerShell window. Then, press Enter and you will see the entries in the session history; each command will be displayed with its ID in the front.

How to use your command history in windows powershell

You can get desired entries and information by adding a pipeline operator (|) and the command after the Get-History. For instance, Get-History | Where-Object .

To see the execution status of each command (with start and end times) in the command history, you should type Get-History | Format-List -Property * and press Enter.

* 4. Invoke-History

If you’re careful enough, you’ll find there’s an ID (exact number) in front of each command you typed. This is very useful when you want to execute a certain command again.

  1. Go to check the ID of the certain command you want to run.
  2. Type Invoke-History ID (replace ID with the number of certain command).
  3. Press Enter on the keyboard and wait for the command to finish.

How to Clear, Export, and Import PowerShell Command History

Clear commands:

Export the commands (Export-Clixml or Export-CSV):

Get-History | Export-Clixml -Path

Get-History | Export-Csv -Path

Imports the commands:

Add-History -InputObject (Import-Clixml -Path

Add-History -InputObject (Import-Csv -Path

Please make sure you have typed the right commands when viewing PowerShell history.

  • Facebook
  • Twitter
  • Linkedin
  • Reddit

ABOUT THE AUTHOR

How to use your command history in windows powershell

Position: Columnist

Sarah is working as editor at MiniTool since she was graduated from university, having rich writing experiences. Love to help other people out from computer problems, disk issues, and data loss dilemma and specialize in these things. She said it’s a wonderful thing to see people solving their problems on PC, mobile photos, and other devices; it’s a sense of accomplishment. Sarah likes to make friends in life and she’s a huge music fan.

Gets a list of the commands entered during the current session.

Syntax

Description

The Get-History cmdlet gets the session history, that is, the list of commands entered during the current session.

PowerShell automatically maintains a history of each session. The number of entries in the session history is determined by the value of the $MaximumHistoryCount preference variable. Beginning in Windows PowerShell 3.0, the default value is 4096 . By default, history files are saved in the home directory, but you can save the file in any location. For more information about the history features in PowerShell, see about_History.

The session history is managed separately from the history maintained by the PSReadLine module. Both histories are available in sessions where PSReadLine is loaded. This cmdlet only works with the session history. For more information see, about_PSReadLine.

Examples

Example 1: Get the session history

This example gets the entries in the session history. The default display shows each command and its ID, which indicates the order in which they ran.

Example 2: Get entries that include a string

This example gets entries in the command history that include the string service. The first command gets all entries in the session history. The pipeline operator ( | ) passes the results to the Where-Object cmdlet, which selects only the commands that include service.

Example 3: Export history entries up to a specific ID

This example gets the five most recent history entries ending with entry 7. The pipeline operator passes the result to the Export-Csv cmdlet, which formats the history as comma-separated text and saves it in the History.csv file. The file includes the data that is displayed when you format the history as a list. This includes the status and start and end times of the command.

Example 4: Display the most recent command

This example gets the last command in the command history. The last command is the most recently entered command. This command uses the Count parameter to display just one command. By default, Get-History gets the most recent commands. This command can be abbreviated to “h -c 1” and is equivalent to pressing the up-arrow key.

Example 5: Display all the properties of the entries in the history

This example displays all of the properties of entries in the session history. The pipeline operator passes the results of a Get-History command to the Format-List cmdlet, which displays all of the properties of each history entry. This includes the ID, status, and start and end times of the command.

Parameters

Specifies the number of the most recent history entries that this cmdlet gets. By, default, Get-History gets all entries in the session history. If you use both the Count and Id parameters in a command, the display ends with the command that is specified by the Id parameter.

In Windows PowerShell 2.0, by default, Get-History gets the 32 most recent entries.

Type: Int32
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

Specifies an array of the IDs of entries in the session history. Get-History gets only specified entries. If you use both the Id and Count parameters in a command, Get-History gets the most recent entries ending with the entry specified by the Id parameter.

Type: Int64 [ ]
Position: 0
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

Inputs

You can pipe a history ID to this cmdlet.

Outputs

This cmdlet returns a history object for each history item that it gets.

Notes

The session history is a list of the commands entered during the session. The session history represents the run order, the status, and the start and end times of the command. As you enter each command, PowerShell adds it to the history so that you can reuse it. For more information about the command history, see about_History.

Starting in Windows PowerShell 3.0, the default value of the $MaximumHistoryCount preference variable is 4096 . In Windows PowerShell 2.0, the default value is 64 . For more information about the $MaximumHistoryCount variable, see about_Preference_Variables.

Jun 13, 2018
Comment

PowerShell on Windows 10 can give you a history of every command you’ve executed in the current session however, for many users this isn’t enough. They need a history of commands executed across sessions. If you need to get the PowerShell command history, and history for the current session alone doesn’t do the trick, there’s a script and a log file that can help you.

Command History Current Session

If you’re running PowerShell 5, you can get the command history for the current session by running the following command;

By default, PowerShell can save up to 50 commands but you can change it to save more. Open PowerShell and right-click the title bar. From the context menu, go to Properties. On the Properties window, go to the Options tab. You will see a Command History section where the default is set to 50. Change it to a higher value.

How to use your command history in windows powershell

PowerShell History Log

In order to view the history log, you need to first install the PSReadLine module with the following command;

Next, run the following command and it will return a file location. The file at this location will give you a complete history of every command you’ve run in PowerShell.

How to use your command history in windows powershell

Cross-Session PowerShell Command History

This is a bit of a long process but it’s worth it. This solution comes from Intel. First open the following location and check if there is a file named Microsoft.PowerShell_profile.ps1 at this location.

If there’s no file there, open PowerShell and run the following command. It will open your Profile file in Notepad but that file doesn’t exist and PowerShell will tell you as much, and ask if you want to create it. Allow it to create the file.

Close the notepad file that opens. In PowerShell, run this command to make sure you’re running PowerShell 5.

Next, set your execution policy to RemoteSigned with the following command.

Next, you need to install the PsUrl and PSReadline modules. You can install them with the following command though if you get an error, just install them manually by the process outlined on the linked pages, or try adding the repository as a trusted repository and then running the command again.

Now that those modules have been installed, open this file;

Paste the following inside this file, and save it.

In order to view command history in PowerShell, you need to run the Get-History command however there is one very crucial step to making sure your history is saved. You CANNOT close PowerShell by clicking the close button. Instead you must always close it with the exit command.

How to use your command history in windows powershell

You can use the Up and Down arrow keys to cycle through the previous commands.

I use current Windows 10 with Powershell 5.1. Often, I want to look up commands I have used in the past to modify and/or re-run them. Inevitably, the commands I’m looking for were run in a previous or different PowerShell window/session.

When I hammer the ↑ key, I can browse through many, many commands from many, many sessions, but when I try to search through them using Get-History | Where-Object <$_.CommandLine -Like "*docker cp*">, I get no results. Basic troubleshooting reveals that Get-History doesn’t show anything from previous sessions, as shown by:

How can I search through the previous commands that the ↑ key provides using Get-History or another Cmdlet?

4 Answers 4

The persistent history you mention is provided by PSReadLine. It is separate from the session-bound Get-History .

The history is stored in a file defined by the property (Get-PSReadlineOption).HistorySavePath . View this file with Get-Content (Get-PSReadlineOption).HistorySavePath , or a text editor, etc. Inspect related options with Get-PSReadlineOption . PSReadLine also performs history searches via ctrl + r .

Using your provided example:

  • Press Ctrl + R and then start typing, to search backward in history interactively. This matches the text from anywhere in the command line. Press Ctrl + R again to find next match.
  • Ctrl + S works like above, but searches forward in history. You can use Ctrl + R / Ctrl + S to go back and forth in search results.
  • Type a text and then press F8 . This searches for the previous item in the history that starts with the current input.
  • Shift + F8 works like F8 , but searches forward.

More Info

As @jscott mentioned in his/her answer, PowerShell 5.1 or higher in Windows 10, uses the PSReadLine module to support command editing environment. The full key mapping of this module can be retrieved by using Get-PSReadLineKeyHandler cmdlet. To view all the key mappings related to history, use the following command:

Summary : Microsoft Scripting Guy, Ed Wilson, talks about avoiding Windows PowerShell scripting by using command history.

Hey, Scripting Guy! I used to have a program that I could use to keep track of commands I typed at the command prompt. It permitted me to use macros to replay those commands or to select which commands I wanted to run. It was simple, elegant, and not complicated. It is a shame that “modern” programs are not that easy to use. I really don’t relish the thought of having to learning scripting to be able to run a few commands. I wish I could go back to my old command prompt. But I guess I cannot. No action required. I am just letting off steam today. Thanks for listening.

Hello LM,

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I were out and about the other day, before the snow storm hit, and we ran across a little tea shop that had a very nice Lapsang Souchong tea. It is one of the few teas that I drink with milk. I find I need the milk to help cut the rather strong, unique flavor. If you haven’t had this tea, you may want to try it sometime—but don’t rush out and buy a pound of it just yet. It is a bit of an acquired taste, and you might not like it.

It is a good tea to try off a nice tea menu to see if you like it. I find it goes really well with Scottish butter cookies—but again, that may be me. I find the combination to be a bit “old school”; but then, it may be that I fell in love with the combination a long time ago when we were in Australia for the first time.

Speaking of old school…

Yeah, I think I may remember using an old-fashioned command prompt a long time ago. It was simple—but I also remember some of the commands were pretty convoluted, and they all seemed to act differently. The good thing, LM, is that you can use the Windows PowerShell console in a similar fashion to how you used the command line. You do not have to learn scripting if you do not want to. All you need to do is to use the command history.

Windows PowerShell command history

There are a number of Windows PowerShell cmdlets that permit working with the command history. I can find them by using the Get-Command cmdlet, and then looking for a name that matches History and a command type that equals Cmdlet. Here is the command and the results:

PS C:\> Get-Command -Name *history* -CommandType cmdlet

CommandType Name ModuleName

Cmdlet Add-History Microsoft.Powe…

Cmdlet Clear-History Microsoft.Powe…

Cmdlet Get-History Microsoft.Powe…

Cmdlet Invoke-History Microsoft.Powe…

To see the commands in my history, I use the Get-History cmdlet:

PS C:\> Get-History

11 Get-Command -Noun history

13 Get-Command -Noun history -CommandType cmdlet

14 Get-Command -Noun history -CommandType Cmdlet

15 Get-Command -Noun history

16 Get-Command Get-Command -Syntax

18 Get-Command -Name *history* -CommandType cmdlet

If I find myself using the Get-History cmdlet often, I can use an alias. Here is the Get-Alias command:

PS C:\> Get-Alias -Definition Get-History

CommandType Name ModuleName

Alias ghy -> Get-History

Alias h -> Get-History

Alias history -> Get-History

If I want to run a command from the history, I use the Invoke-History cmdlet. I generally use Get-History and Invoke-History together. I need to see what is in my history before I can execute a prior command. The easiest way to run something from the history is to specify the command ID. This technique is shown here:

PS C:\> Get-History

25 Get-Service bits

26 Get-Process explorer

PS C:\> Invoke-History -Id 25

Status Name DisplayName

Running bits Background Intelligent Transfer Ser…

As shown here, I can specify an array of ID numbers for the Get-History cmdlet:

PS C:\> Get-History -Id 25, 26

25 Get-Service bits

26 Get-Process explorer

But when I attempt to use an array of IDs with the Invoke-History cmdlet, I get the following error message:

How to use your command history in windows powershell

Well, I can pipe the results from Get-History to Invoke-History:

PS C:\> Get-History 25 | Invoke-History

Status Name DisplayName

Running bits Background Intelligent Transfer Ser…

Because I can get multiple items from the history, can I also pipeline them? As shown in the following image, I still get an error message:

How to use your command history in windows powershell

When I see an error message that says a cmdlet will not accept multiple items through the pipeline, I can usually substitute the command for one that includes Foreach-Object. This is because Foreach-Object interrupts the pipeline and deals with each item, one at a time. It ruins “streaming” the output, but it permits me to pipe the results from one command to another. I can also automate the output without having to resort to scripting. Here is the revised command:

Get-History -Id 25, 26 | foreach

As shown in the following image, the command works:

How to use your command history in windows powershell

LM, that is all there is to using the Windows PowerShell command history. Script Without Scripting Week will continue tomorrow when I will talk about more cool stuff.

January 22nd, 2011

Summary: Use the Export-History Windows PowerShell function to simplify saving command history.

Microsoft Scripting Guy Ed Wilson here. One of the things I was thinking about when I was writing the article this week about the History cmdlets was that I wish it were a bit easier to export my history. This gave me an idea: Why don’t I write a function to simplify exporting my command history? I decided it would be cool if I could type Export-History and a path to the XML file. In addition, I thought it is important to be able to cherry-pick which commands I want to include in the history file. After all, opening a new Windows PowerShell console and attempting to retype a whole bunch of commands perfectly can be a pain.

With these two design criteria in mind, I came up with the Export-History function shown here.

HelpMessage=”Type Path for history file”)]

Get-History | Export-Clixml -Path $path

> #end function Export-History

# Get-History (6,7) | Export-History -Path c:\fso\mycommands.xml

# Export-History -Path c:\fso\a.xml

The Get-History function begins by creating two parameters. The first parameter is the commandInfo parameter. It will accept a value from the pipeline, and is not mandatory. The second parameter is the path parameter, and it is mandatory. It tells the function where to save the XML file. This portion of the function is shown here.

HelpMessage=”Type Path for history file”)]

Now I need to add a little bit of logic. If the commandInfo parameter is not supplied, I want to export the entire history buffer from the current Windows PowerShell session. If, on the other hand, I call the Export-History function and take the trouble to select specific history commands, the function accepts the history information from the pipeline. This is the applicable portion of the function:

Get-History | Export-Clixml -Path $path

> #end function Export-History

At the bottom of the Export-History.ps1 file, I have two examples of calling the function. One illustrates calling the function with no commandInfo, and the other pipelines the commandInfo to the function. These two commands are shown here:

# Get-History (6,7) | Export-History -Path c:\fso\mycommands.xml

# Export-History -Path c:\fso\a.xml

So, how do you actually use the Export-History function? For one thing, I can dot-source it into my Windows PowerShell console session. This is shown in the following image and it allows me to then run the function like any other Windows PowerShell cmdlet. I type Export-History and am prompted for the path to save the command history XML file. This is because I marked the path parameter as mandatory.

After the XML file has been exported, I can use Internet Explorer to review the XML file to see if it contains the commands I wanted to export. The command history XML file appears in the following image. Just for fun, I have drawn arrows to the two commands in which I am interested.

You can open the script in the Windows PowerShell ISE, and edit one of the command lines. After this is accomplished, you can run the script. This is shown in the following image.

You can leave the two commands commented out, and simply run the Export-History function in the Windows PowerShell ISE console. Then, in the immediate window (the bottom pane), you can type the command as you would in the Windows PowerShell console, or as the command might appear in the script itself. What you have accomplished is to load the function onto the function drive (the same as dot-sourcing in the Windows PowerShell console). You can then refer to the function whenever you wish. This technique is illustrated in the following image (I have drawn an arrow to point to the command pane).

You could also include the Export-History function in your Windows PowerShell profile. This is the option I like the best. I have written several Hey, Scripting Guy! Blog posts that talk about using the Windows PowerShell profile. Refer to them for ideas about incorporating the Export-History function. Join me tomorrow as I develop an Import-History function.

On Windows, you can track user login and logoff events using the Security log. In this article, we’ll show you how to get user login/logoff history from Event Logs on the local computer using simple PowerShell script.

In order the user logon/logoff events to be displayed in the Security log, you need to enable the audit of logon events using Group Policies.

You can enable login auditing on all domain-joined computers using a domain GPO.

  1. Run the Group Policy Management Console under domain admin account (gpmc.msc);
  2. Right-click on Default Domain Policy and select Edit; How to use your command history in windows powershell
  3. Go to the following GPO section: Computer Configuration > Policies > Windows Settings > Advanced Audit Policy Configuration > Audit Policies > Logon/Logoff;
  4. Enable the following GPO options: Audit Logoff, Audit Logon, Audit Other Logon/Logoff Events. To do this, in each policy, select the options Configure the following audit events >Success; How to use your command history in windows powershell
  5. Save the GPO and wait until the new policy settings are applied to the domain computers (you can apply the policy on a client immediately using the gpupdate command ).

Now, when a user logons locally or remotely to a computer, an event with EventID 4624 appears in the Windows Logs > Security event log.

You can manually filter all logon events with the specified code in the Event Viewer. Run the Compute Management console. Go to System Tools > Event Viewer > Windows > Logs > Security. Right-click on this section and select Filter Current Log. In the window that opens, specify Event ID 4624 and click OK.

How to use your command history in windows powershell

As a result, only user logon events will be displayed in the event log. Open any Audit Success event. The event description says “An account was successfully logged on”. The name of the user who logged in is specified in the following message field:

Security ID: CORPjsmith

Account Name: jsmith

If the user has logged on from a remote computer, the name (or IP) of the computer will be specified in the: Source Network Address: 192.168.1.70

How to use your command history in windows powershell

Let’s try to use PowerShell to select all user logon and logout events. To select events with EventID 4634 and 4624, we use the Get-WinEvent cmdlet. The following PowerShell script must be run with elevated privileges.

$logs =Get-WinEvent -LogName Security| Where-Object

Short Description

Describes how to get and run commands in the command history.

Long Description

When you enter a command at the command prompt, PowerShell saves the command in the command history. You can use the commands in the history as a record of your work. And, you can recall and run the commands from the command history.

PowerShell has two different history providers: the built-in history and the history managed by the PSReadLine module. The histories are managed separately, but both histories are available in sessions where PSReadLine is loaded.

Using the PSReadLine history

The PSReadLine history tracks the commands used in all PowerShell sessions. The history is written to a central file per host. That history file is available to all sessions and contains all past history. The history is not deleted when the session ends. Also, that history cannot be managed by the *-History cmdlets. For more information, see about_PSReadLine.

Using the built-in session history

The built-in history only tracks the commands used in the current session. The history is not available to other sessions and is deleted when the session ends.

History Cmdlets

PowerShell has a set of cmdlets that manage the command history.

Cmdlet Alias Description
Get-History h Gets the command history.
Invoke-History r Runs a command in the command history.
Add-History Adds a command to the command history.
Clear-History clhy Deletes commands from the command history.

Keyboard Shortcuts for Managing History

In the PowerShell console, you can use the following shortcuts to manage the command history.

  • UpArrow – Displays the previous command.
  • DownArrow – Displays the next command.
  • F7 – Displays the command history.
  • ESC – To hide the history.
  • F8 – Finds a command. Type one or more characters then press F8 . Press F8 again the next instance.
  • F9 – Find a command by history ID. Type the history ID then press F9 . Press F7 to find the ID.
  • # Tab – Search the history for * * and returns the most recent match. If you press Tab repeatedly, it cycles through the matching items in your history.

These key bindings are implemented by the console host application. Other applications, such as Visual Studio Code or Windows Terminal, can have different key bindings. The bindings can be overridden by the PSReadLine module. PSReadLine loads automatically when you start a PowerShell session. With PSReadLine loaded, F7 and F9 are not bound to any function. PSReadLine does not provide equivalent functionality. For more information, see about_PSReadLine.

MaximumHistoryCount

The $MaximumHistoryCount preference variable determines the maximum number of commands that PowerShell saves in the command history. The default value is 4096.

For example, the following command lowers the $MaximumHistoryCount to 100 commands:

To apply the setting, restart PowerShell.

To save the new variable value for all your PowerShell sessions, add the assignment statement to a PowerShell profile. For more information about profiles, see about_Profiles.

For more information about the $MaximumHistoryCount preference variable, see about_Preference_Variables.

Order of Commands in the History

Commands are added to the history when the command finishes executing, not when the command is entered. If commands take some time to be completed, or if the commands are executing in a nested prompt, the commands might appear to be out of order in the history. Commands that are executing in a nested prompt are completed only when you exit the prompt level.