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.)

command history

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?

Recover Files Using CMD: Easy and Portable Ways
Recover Files Using CMD: Easy and Portable Ways

How to recover files using CMD? This post gives you detailed steps for data recovery in different situations.

Read More

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.

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.

F8

Tip: The search term (the part you have typed) will be highlighted with a fluorescent green; and the rest of the command will also be displayed next to it.

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.

search back or forward

Note: There will be a snippet at the bottom of the window to show you the status, indicating whether you’re searching back or forward (bck-i-search or fwd-i-search).

* 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.

Get-History

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 {$_.CommandLine -like “*Service*”}.

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.

Invoke-History

How to Clear, Export, and Import PowerShell Command History

Clear commands:

Clear-History

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

Get-History | Export-Clixml -Path <Path>\PSHistorycommands.xml

Get-History | Export-Csv -Path <Path>\PSHistorycommands.csv

Imports the commands:

Add-History -InputObject (Import-Clixml -Path <Path>\PSHistorycommands.xml)

Add-History -InputObject (Import-Csv -Path <Path>\PSHistorycommands.csv)

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

  • linkedin
  • reddit