How to use command history in Windows PowerShell

Windows PowerShell has a built-in command history feature that provides detailed information about the commands you 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 is the command line buffer, which is actually part of the graphical PowerShell terminal application and not part of the underlying Windows PowerShell application. It provides some basic features:

Read more:

What is Powershell 7?
How to Connect to Exchange Online via Powershell?
Enabling script execution in PowerShell
Deploy Azure VM to Powershell
Schedule a reboot on Windows Server

  • Up arrow : Retrieve the previous command you entered. Press the key repeatedly to cycle through the command history.
  • Down arrow : Retrieve the next command entered. Press the key repeatedly to cycle through the command history.
  • F8 : Search the command history for a command that matches the text on the current command line. So, if you wanted to search for a command that started with "p", you would type "p" on the command line and then repeatedly tap F8 to cycle through the commands in your history that start with "a".

By default, the buffer remembers the last 50 commands you entered. To change this, right-click on the title bar of the PowerShell prompt window, select "Properties" and change the "Buffer size" value in Command History.

Viewing PowerShell History

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

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

Get-History

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

Get-History | Select-String -Pattern "Example"

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

Get-History | Format-List -Property *

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

Get-History -Count 1000 | Select-String -Pattern "Example"

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 byGet-History.cmdlet:

Invoke-History #

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 would run:

Invoke-History 1; Invoke-History 2

How to clear your PowerShell history

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

Clear-History

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

PowerShell does not remember your history between sessions. To delete both command histories from the current session, all you need to do is close the PowerShell window.

If you want to clear the PowerShell window after clearing the history, run the command Clear:

Clear

Saving and importing PowerShell history

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

Get-history | Export-Clixml -Path c: /commands.xml

Was this article helpful?

To maintain a quality standard for you, we have invested in a great hosting plan, Paid CDN, Website Optimization Plugins, etc ...

Help us to keep the project active! 

Follow the news in real time. Follow our Instagram profile..

Felipe Santos
Felipe Santos is a Cloud and Security Architect, with experience in Windows Server, Cluster, Storages, Backups Veeam and Office 365 environments.
en_USEnglish

UP TO 90% DISCOUNT

TAKE OFF YOUR CAREER !!

Do you want to upgrade your career? 

Invest in yourself and get ahead! Get that dream job in 2022!