Exploiting Your Command History

by Nov 8, 2016

PowerShell “records” all your interactive command input to its command history, and Get-History shows them. If you played around with PowerShell for a while and then decided that it wasn’t all that bad what you did, here is a script that copies all interactive commands from command history to your clipboard. You can then paste it into the PowerShell ISE, and make it a script:

# define how old your commands may be at most to be included
$MaxAgeHours = 4

# get all command history items that were started after this
$DateLimit = (Get-Date).AddHours(-$MaxAgeHours)

# get all command-line commands
Get-History |
  # exclude all that were aborted
  Where-Object ExecutionStatus -eq Completed |
  # exclude all that are older than the limit set above
  Where-Object StartExecutionTime -gt $DateLimit |
  # get just the command-line
  Select-Object -ExpandProperty CommandLine |
  # copy all to clipboard

Twitter This Tip! ReTweet this Tip!