PowerShell How-To

How To Find and Use PowerShell History To Save Time

By using the various history commands in PowerShell, you can search for and easily execute any command that you have previously run in the same session.

If you're like me, you're in the PowerShell console a lot. I use PowerShell for all kinds of different tasks, and I'm always executing long commands that I always, inevitably, have to run again but can't remember.

Instead of racking my brain trying to remember, I instead use the console history to discover and execute those commands again.

There are a few ways to search the console history to find commands you've previously run. One of the easiest is to use the pound (#) symbol. To use this method, just type the pound symbol in your console and start hitting the tab key. This method allows you to cycle through all your history and find that command you forgot all about.

You can also use the Get-History cmdlet in a more formal way. Get-History queries your session history and returns each line with a specific ID.

PS C:\> get-history

  Id CommandLine
  -- -----------
   1 Find-Module -Name xActiveDirectory | Install-Module
   2 Get-Command -Module xActiveDirectory
   3 Import-Module xActiveDirectory
   4 Get-Command -Module xActiveDirectory
   5 Get-Module xActiveDirectory
   6 Find-Module -Name xActiveDirectory | Install-Module
   7 Find-Module -Name azure | Install-Module

Using the Get-History command allows you to narrow down the command you're looking for by querying for a specific ID or, more likely, by the actual command. For instance, maybe you know you did something with an Azure cmdlet a while back but can't remember what it is. You could pipe Get-History's output to Where-Object to return only those commands that had the word "azure" in them.

Using the Where-Object cmdlet's filter script parameter, you're able to query a command based on nearly any criteria.

PS C:\> Get-History | Where-Object { $_.CommandLine -match 'azure' }

  Id CommandLine
  -- -----------
   7 Find-Module -Name azure | Install-Module
   8 Find-Module -Name azure | Install-Module -Force

Say you've found the command you need and want to execute it again. In the above example, I was able to find two instances in my history where I used the word "azure." The IDs are 7 and 8. I'd like to rerun the #7 command. To do that, I could copy that line and paste it into the console, but there's a better way. I could use Invoke-History.

Invoke-History is a little-known command that performs a simple function. It has a single parameter called Id which represents the command ID that should be executed. Below, you can see that I'm specifying the #7 command ID. Invoke-History then returns the command that it will be executing and then does it right after.

PS C:\> Invoke-History -Id 7
Find-Module -Name azure | Install-Module

If you find yourself sifting through a lot of history to find certain commands and notice that you don't need any of it anymore, you could also use Clear-History. The Clear-History command does exactly what you'd expect: It removes all of the history and lets you start from scratch.

PS C:\> Clear-History
PS C:\> get-history

  Id CommandLine
  -- -----------
  22 Clear-History

By using the various history commands in PowerShell, you're able to search for and easily execute any command that you have previously run in the same session.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular