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 192.168.86.92 -InterfaceAlias 'Ethernet 2' -AddressFamily IPv
  10 Set-NetIPAddress -IPAddress 192.168.86.92 -InterfaceAlias 'Ethernet 2'
  11 New-NetIPAddress –InterfaceAlias 'Ethernet 2' –IPv4Address '192.168.86.92' –PrefixLength
  12 New-NetIPAddress –InterfaceAlias 'Ethernet 2' -IPAddress '192.168.86.92' –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 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