Don’t Script: Edit PowerShell Command History


Summary: Microsoft Scripting Guy, Ed Wilson, talks about editing the Windows PowerShell command history.

Hey, Scripting Guy! Question 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?

—AP

Hey, Scripting Guy! Answer Hello AP,

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

PS C:\>

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

  Id CommandLine

  --   -----------

  67 srv

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

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

+ srv

+ ~~~

    + 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'

  Id CommandLine

  --   -----------

  67 srv

  78 srv

  81 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'

  Id CommandLine

  --   -----------

  78 srv

  81 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:

Image of command output

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 scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (5)

  1. Anonymous says:

    Greg –
    – Could you elaborate on the use of ‘#’ to edit a previously-used command line?
    – I have PSReadLine installed — very useful, indeed — but I guess I’ll have to dig deeper to see if it can achieve what I am looking for: Instead of up-arrowing 20+ times to make a previous command current for editing, I’d like to pick it out of the Get-History
    list.
    Thanks —

  2. Greg Wojan says:

    You’re working way to hard… 😉

    On PowerShell 2+ simply type "#" and press . The command line will be editable from that point.

    Even better still, use the EXCELLENT PSReadLine module and go nuts! Absolutely awesome.

  3. CB says:

    I wrote a function to enable editing of recalled command lines.
    It’s not perfect — it doesn’t handle ‘$’ correctly, for example — but very handy otherwise.
    function Edit-CmdFromHistory {
    param (
    [Parameter(Mandatory=$false)]
    [Int32] $CmdCnt = 15
    )
    $cmds=get-history | select -last $CmdCnt;
    $cmds;

    Write-Host "Enter a cmd id to edit (empty to cancel)" -Foreground Yellow;
    $id = Read-Host " ";
    if([string]::IsNullOrEmpty($id) -eq $false) {
    $title = $Host.UI.RawUI.WindowTitle;
    $c = Get-History $id;
    $wshell = New-Object -ComObject wscript.shell;

    $result = $wshell.AppActivate($title)

    $keys = Handle-BracesForSendKeys $c.CommandLine;

    $wshell.SendKeys($keys)
    }
    else {
    "Canceled.";
    }
    <#
    .SYNOPSIS
    A concatenation of Get-History/Invoke-History in one command, but allows editing of command before executing.
    Display last CmdCnt number of commands. Allows user to select by number and pastes the command on the command line for editing.

    .EXAMPLE

    .PARAMETER CmdCnt
    (Optional) Number of commands to display. Default = 15.

    .NOTES
    Note See Run-CmdFromHistory

    #>
    }
    #
    # SendKeys interprets ‘{‘ and ‘}’ as enclosing special names ("{Enter}", for example).
    # Surround them in ‘{}’ to make them literal again.
    #
    function Handle-BracesForSendKeys {
    param (
    [Parameter(Mandatory=$true)]
    [string] $Str
    )
    $new = $Str -replace ‘([{}])’, ‘{$1}’
    # " $new"
    $new

    }

  4. Sepatu Running says:

    You can read my article. thank you

  5. Sepatu Murah says:

    You can read my article. thank you
    help promote my merchandise.
    clik here http://www.centralsepatu.com

Skip to main content