PowerShell How-To

Working with PowerShell History

Using these three commands, PowerShell allows you to read and manipulate just about every aspect of your command history.

With any good shell, it's important to keep a history of all commands that have been executed in the past. Being able to recall a complicated command that was used in the past is a huge time-saver.

PowerShell is a Windows shell that provides a history of commands that are easily accessible via a few common commands.

The commands to interact with PowerShell history are Get-History, Clear-History and Invoke-History. We technically have the Add-History command, as well, but its uses are minimal, and you will rarely (if ever) need it.

Probably the most common history command is Get-History. This command returns all of the commands you've executed in your current session, meaning if you close your console, the session starts over. Below is an example of the output you'll see from Get-History.

PS> Get-History

  Id CommandLine
  -- -----------
   1 get-service
   2 get-service -Name wudfsvc
   3 start-service -Name wudfsvc
   4 Get-NetIPAddress
   5 Get-NetIPAddress
   6 Get-NetIPAddress -AddressFamily IPv4
   7 Get-NetIPAddress -AddressFamily IPv4 -Type Unicast
   8 Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias 'Ethernet 2'
   9 Set-NetIPAddress -IPAddress -InterfaceAlias 'Ethernet 2' -AddressFamily IPv
  10 Set-NetIPAddress -IPAddress -InterfaceAlias 'Ethernet 2'
  11 New-NetIPAddress –InterfaceAlias 'Ethernet 2' –IPv4Address '' –PrefixLength
  12 New-NetIPAddress –InterfaceAlias 'Ethernet 2' -IPAddress '' –PrefixLength 24
  13 connect-azurerm
  14 Get-AzureRmAppServicePlan -ResourceGroupName bdt099
  15 Get-AzureRmAppServicePlan -ResourceGroupName bdt099
  16 gcm *history*
  17 Get-History

Each history entry has an ID associated with it. This ID allows you to selectively choose one or more lines if you want only to return them. Using the Id parameter, I can return only a couple lines if I want to, like Get-History -Id 1,10,16.

Get-History shows the history, but when you're looking through the history, chances are you'd like to execute that command again. This is what Invoke-History is for. Instead of having to copy/paste commands from the history, you can use Invoke-History to execute that command directly from the history.

Below, I'm executing the first entry in my history. This command was running Get-Service. You can see how Invoke-History executes whatever command was run at that specific ID.

PS> Invoke-History -Id 1

Status   Name               DisplayName
------   ----               -----------
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Running  AppIDSvc           Application Identity

Perhaps your history is getting too overwhelming and you'd like to remove it entirely. You can do this with Clear-History. You can run Clear-History with no parameters and it will remove everything or choose to remove a specific entry with the Id parameter or perhaps clear the last few lines only with the Newest parameter.

By using these three commands, PowerShell allows you to read and manipulate just about every aspect of your command history.

About the Author

Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.