Wrapping History

posh-wrapper2

Once upon a time, well actually just five months ago, I posted a blog entry about wrapping code or file contents as a Windows PowerShell function. At the time I thought I’d post the next day or week with more details. Finally, here’s a bit more of the story.

First, a little review. Consider the following question:

“I’ve typed a lot of great commands in PowerShell just now. How do I save those to a file to run again as a script? Actually, if I could save all or part of my recent commands as a function in my profile, that would be great! Is that even possible?”

 

Previously, we looked at a Wrap-Function filter I’d written. Now let’s focus more directly on the question. When you need to deal with recent commands you’ve typed in Windows PowerShell, two facilities come to mind:

  1. History
  2. Transcripts

While using transcripts (see the cmdlet Start-Transcript) are wonderful, we’ll first focus on history. PowerShell keeps a history of the recent commands which have been entered. For each command, the history mechanism records an identifier number (Id) for the command within the current session, what you typed (CommandLine), whether the command succeeded or not (ExecutionStatus), and when the command started and stopped running (StartExecutionTime, EndExecutionTime). The cmdlets Add-History, Get-History, and Invoke-History work with such history information. Also, the alias “history” provides a shorthand for the Get-History cmdlet which might seem reminiscent of a particular history command from certain UNIX-heritage shells.

Let’s focus on Get-History, because Add-History and Invoke-History are just far too much fun for us to be distracted by at the moment. Get-History just shows us what commands we’ve recently run. How do we turn those into a function or script? With a little bit of magic of course. Read on true fans.

function global:Wrap-History { 
    param( $count=32, $fun=”Wrapped” ) 
    Get-History -Count $count `
    | ForEach-Object { $_.CommandLine } `
    | Wrap-Function $fun 
}

 

This Wrap-History function is a simplified form of one which will write the function to a script file is so desired, but it’s certainly functional as it is shown here.

How does it work? It uses the Get-History cmdlet to retrieve recently entered commands. The optional count parameter allows choosing how many commands should be included. Then it pipes that history information to the ForEach-Object cmdlet to select only the CommandLine property of the history objects. Next the CommandLine attribute of each of the recent history items is piped to the Wrap-Function filter which is given the optionally supplied name or a default name of “Wrapped.” Here is where this version of the Wrap-History function ends. If you’d like to redirect the output to a file, this can simplify saving the resultant function as a script file.

Stay tuned for more on wrapping functions (part 3).

One thought on “Wrapping History”

  1. Well we did have “help.exe” that at least produced a list of all the commnads available to you (try it on cmd, works even today ) though there was no formal way of registering your own exe as a “command” Ok so I still have a cmd.exe hangover (and yes I love the $variables thanks to my PERL legacy!)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.