SelectHistory.ps1 – Extracting Commands from History

I wanted a way to be able to pick both individual lines from my history as well as ranges.  This is handy if you have been playing around at the prompt and want to copy a number of individual commands to a script file.  Now you could do this with the Start-Transcript cmdlet but you have to execute Start-Transcipt *before* you start playing around at the prompt.  Unfortunately I don’t always know ahead of time that I’m going to do something that I want to capture to a script file.  So here is a trivial little function I wrote to select ranges of history and potential discontigous entries in the history using the range syntax.  For example:
 
PSH> SelectHistory (1..5+6,9+20..25) > foo.ps1
 
Here is the script contents for SelectHistory.ps1:
 
param([int[]]$indices)
if ($indices -eq $null) {
    "Usage: SelectHistory (range)"
    ""
    "Example: SelectHistory (2..11+13,15)"
    ""
    return
}
$indices | foreach { (get-history -id $_).CommandLine }
Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

2 Responses to SelectHistory.ps1 – Extracting Commands from History

  1. Sung says:

    That\’s a nice little utility script that I could use often ;)I was playing around with the script and wanted to just specify some large number.  the script complained but all the history entries from 1 thru 5 are written to "foo.txt" but not sure how to suppress the error message[^_^]PS[59]>.\\SelectHistory.ps1 (1..5+100000) > foo.txtGet-History : Cannot locate (history:100000).At C:\\programming\\ps\\snapins\\SelectHistory.ps1:9 char:34+ $indices | foreach { (get-history  <<<< -id $_).CommandLine }[^_^]PS[60]>type foo.txtgps svchost | select name,servicegps svchost | select servicegps svchost | select service | gmclsgps svchost | select name,id,serviceMy $MaximumHistoryCount is set to "64" so after a while, "Get-History" could not find the first history item.(In the prompt, number in [] is the history count, in this case, it\’s 66)[^_^]PS[66]>.\\SelectHistory.ps1 (1..5+10000) > foo.txtGet-History : Cannot locate (history:1).At C:\\programming\\ps\\snapins\\SelectHistory.ps1:9 char:34+ $indices | foreach { (get-history  <<<< -id $_).CommandLine }Get-History : Cannot locate (history:10000).At C:\\programming\\ps\\snapins\\SelectHistory.ps1:9 char:34+ $indices | foreach { (get-history  <<<< -id $_).CommandLine }Have you improved the script lately to get around the error message and $MaximumHistoryCount by chance?  

  2. Keith says:

    I don\’t know that you can work around the MaximumHistoryCount.  I set mine to 512 so that PowerShell will store more history.  I think I could make the error handling better.  Let me take a stab at it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s