• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Command History

PowerShell Command History

14/05/2018 by Stephanos 1 Comment

PowerShell Command History

In this tutorial we will see about PowerShell History. We will see want information is kept in history, how we can access and use our history. Every command you run in Windows PowerShell, it is added in the command history. The command is added in the history after the execution of the command has been completed. If a command takes a lot of time to complete the execution, you will not be able to see the command in the command history list unless it has completed the action. PowerShell command history can be displayed in console, or exported in a file. You can use your command history for evidence of you work or reference for procedures that you repeat. In order to see the history you have to use the below command.

Code:

Get-History

Maximum History Count

The default maximum history count of Windows PowerShell is 4096 (PowerShell 3.0 and above). Until PowerShell 2.0 the default limit was only 64. The limit is defined by a preference variable called $MaximumHistoryCount. You are allowed to change the limit by setting the number of the variable to a different one. The accepted range is from 1 to 32767. You can set the limit to a different one by using the command.

$MaximumHistoryCount = [number]

The below example sets the history limit to 1000.

Code:

$MaximumHistoryCount = 1000

The below example sets the limit to 20000.

Code:

$MaximumHistoryCount = 20000

Please note, in order for the new setting to be applied, you need to restart Windows PowerShell. This will apply the new setting only to the current session. You are able also to change the limit on all your sessions. In order to do that you will need to perform the change in Windows PowerShell profile. I will not discuss further on how this can be applied on profile level, but you are able to find more information on Microsoft.

[adinserter name=”In Article”]

History CmdLets

The are a few cmdlets that are related to PowerShell command history. We will see each one of the with examples so you will be able to understand better.

Get-History

Get-History cmdlet will get the history of commands in the current session. By using the command alone it will display the information in the console. You can also export the output to a file or by using Out-GridView. The default alias of the command is h. In the below screenshots you will see the output of the cmdlet and the use of the alias.

get-history

h-alias

Invoke-History

By using Invoke-History cmdlet, you will be able to run a command in the command history. The default alias of the command is r. The command needs to be specified by the command id number in the history. The format of the command is Invoke-History 1 or r 1. The below examples show you the use of this command.

invoke-history

Add-History

Add-History cmdlet adds a command in the history. The way that you are able to use this command is show in the below example. One way of using the command is to add a list of commands that you have already exported to the session. In the below example, I get the current command history and then add the list retrieved to the history again. The command that I have used is the below:

Code:

Get-History | Add-History

 

add-history

Clear-History

Clear-History cmdlet clear the history of the current session. The alias of this cmdlet is clhy. Please note that the id number of the commands in history is not reset and will continue from the number that it was left of. The below example show the use of the command.

clear-history

[adinserter name=”In Article”]

Keyboard Shortcuts

There are some keyboard shortcuts that allow you to display, find and run commands from the history. Let’s check them.

Up Arrow: The Up Arrow key displays the previous command in history.

Down Arrow: The Down Arrow key displays the next command in history.

F7: Displays the history in the console.

ESC: Hides history from console.

F8: Finds a command from the history. You need to type few letters and then hit F8. If there are more that one commands in the history that matching those letters, then by hitting F8 again, it will display the next command.

F9: Finds a command by the id number and by pressing ENTER, it will put the command in the console ready to run.

f7-shortcut

f9-shortcut

f9-result

I hope the tutorial about PowerShell command history is helpful.

Please let me know your comments and thoughts. You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_History | Microsoft Docs
  • Get-History – Microsoft Docs
  • Invoke-History – Microsoft Docs
  • Add-History – Microsoft Docs
  • Clear-History – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Command History
Article Name
PowerShell Command History
Description
PowerShell History. In this tutorial you learn about PowerShell History. The information that is included, how can access and use it.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: Add-History, Clear-History, Get-History, Invoke-History

Reader Interactions

Trackbacks

  1. PowerShell Command History - How to Code .NET says:
    15/05/2018 at 00:01

    […] on May 13, 2018by admin submitted by /u/SConstantinou [link] [comments] No comments […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact