Skip to content

Instantly share code, notes, and snippets.

@toenuff
Last active December 24, 2015 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toenuff/6715170 to your computer and use it in GitHub Desktop.
Save toenuff/6715170 to your computer and use it in GitHub Desktop.
Hgrep is a function that provides similar functionality to the unix commands history |grep something
# http://powertoe.wordpress.com/2013/09/26/powerbits-10-history-grep-or-hgrep-in-powershell/
function hgrep {
param(
[Parameter(Mandatory=$false, Position=0)]
[string]$Regex,
[Parameter(Mandatory=$false)]
[switch]$Full
)
$commands = get-history |?{$_.commandline -match $regex}
if ($full) {
$commands |ft *
}
else {
foreach ($command in ($commands |select -ExpandProperty commandline)) {
# This ensures that only the first line is shown of a multiline command
# You can always get the full command using get-history or you can fork and remove this from the gist
if ($command -match '\r\n') {
($command -split '\r\n')[0] + " ..."
}
else {
$command
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment