Don’t Script: Edit PowerShell Command History
Summary: Microsoft Scripting Guy, Ed Wilson, talks about editing the Windows PowerShell command history.
Hey, Scripting Guy! I liked your ideas in Avoid Scripting: Use PowerShell Command History. My problem is that I seldom (if ever) type commands perfectly the first time—often, not even the second time. This means that I end up with a lot of junk in my command history. I wish there was a way to clean up the command history so it would be more usable. Is there?
Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, I am listening to a Canadian rock group and sipping some pomegranate tea. Of course, pomegranate is not really a tea, and a pomegranate tea is actually an herbal fruit kind of concoction. But hey—it is still nice and refreshing, and it doesn’t have any caffeine in it. Along with chocolate biscotti, it makes a great way to work.
AP, another great way to work is to use your Windows PowerShell command history. It is actually very flexible, and it is pretty easy to edit the history if you want to do so.
Editing the Windows PowerShell command history is perhaps a misnomer. I cannot really edit the command history, but I can delete items from it and add items to it. I cannot edit an actual entry from the command history—well actually, I could, but I would need to export the history to a file, edit the file, and then import the history back in, which is a bit more work than I want to do. (If this was a scenario that I found myself doing on a regular basis, I could write a function to make it more doable.)
How do I remove stuff from my Windows PowerShell command history? I use the Clear-History cmdlet. I will admit that when I first saw this cmdlet, I thought that it…well…cleared all of my history from Windows PowerShell. And it will do that, of course. Here is an example:
PS C:\> Clear-History
I type the command, and nothing returns. If I want to see a bit more information, I can use the standard –Verbose Windows PowerShell parameter. This will tell me that the command is clearing my Windows PowerShell command history. This is shown here:
PS C:\> Clear-History -Verbose
VERBOSE: Performing the operation "Clear-History" on target "This command will clear all the entries from the session history."
As shown here, I can also use the –WhatIf parameter if I am unsure that I want to remove all items:
PS C:\> Clear-History -WhatIf
What if: Performing the operation "Clear-History" on target "This command will clear all the entries from the session history."
But the cmdlet is more flexible than simply emptying my Windows PowerShell history. I could remove specific items from my Windows PowerShell history. Before I remove a specific item from the Windows PowerShell command history, I like to look at the command. I can do this by using the Get-History cmdlet and specify a specific ID number. I then pipe the results to the Format-List cmdlet so I can see more information than the standard output. Here is the standard output:
PS C:\> Get-History 67
Here is the more detailed output:
PS C:\> Get-History 67 | fl *
Id : 67
CommandLine : srv
ExecutionStatus : Completed
StartExecutionTime : 3/2/2015 3:15:36 PM
EndExecutionTime : 3/2/2015 3:15:36 PM
Because a command completed (as did the previous command), it does not mean that it completed successfully. Here is what happens when I try to use the Invoke-History cmdlet (r is an alias) to execute the command:
PS C:\> r 67
srv : The term 'srv' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ CategoryInfo : ObjectNotFound: (srv:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Finding a specific command
So I have a command that doesn’t work in my Windows PowerShell command history. I can find all of these types of commands by using the Get-History command and piping the results to the Where-Object cmdlet. I already know that I have a property called CommandLine that contains my commands. Here is the command I use to find the commands:
PS C:\> Get-History | where commandline -eq 'srv'
Removing the bad commands from history
I thought I might need to pipe the results of my Get-History cmdlet to the Clear-History cmdet. But as it turns out, I don’t need to do that. I can use Clear-History to search for and remove my bad command:
Clear-History -CommandLine 'srv'
Sweet…no errors. Then I decide to use the Up arrow to recall my previous Get-History command. Bummer. I still have a couple of entries remaining:
PS C:\> Get-History | where commandline -eq 'srv'
I remember to use the count. I see that there are two instances of the bad command still in history, so I use the Up arrow and add the count. Here is my command:
Clear-History -CommandLine 'srv' -Count 2
Now when I use the Get-History command, nothing appears.
These commands and their associated output are shown in the following image:
AP, that is all there is to editing the Windows PowerShell command history. Script Without Scripting Week will continue tomorrow when I will talk about using code snippets.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy