Copying Command History as a Tool

by Jul 9, 2014

In a previous tip we illustrated how you can copy the previously entered interactive PowerShell commands to your favorite script editor. Here is a function that makes this even easier. If you like it, you may want to put it into your profile script so you have it handy any time:

function Get-MyGeniusInput
{
  param
  (
    $Count,
    $Minute = 10000
  )

  $cutoff = (Get-Date).AddMinutes(-$Minute)

  $null = $PSBoundParameters.Remove('Minute')
  $result = Get-History @PSBoundParameters |
    Where-Object { $_.StartExecutionTime -gt $cutoff } |
    Select-Object -ExpandProperty CommandLine 

  $count = $result.Count
  $result | clip.exe
  Write-Warning "Copied $count command lines to the clipboard!"
} 

Get-MyGeniusInput by default copies the entire command history to the clipboard. With the parameter -Count you can limit the results to a given number, for example the last 5 commands. And with the parameter -Minute, you can specify the number of minutes that you would like to go back in history.

PS> Get-MyGeniusInput -Minute 25
WARNING: Copied 32 command lines to the clipboard!

PS> Get-MyGeniusInput -Minute 25 -Count 5
WARNING: Copied 5 command lines to the clipboard!

PS>  

Twitter This Tip! ReTweet this Tip!