Create a gist now

Instantly share code, notes, and snippets.

@toenuff /hgrep.ps1
Last active Dec 24, 2015

What would you like to do?
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