Last active
December 24, 2015 00:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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