Skip to content

Instantly share code, notes, and snippets.

@aauren
Created November 27, 2012 22:33
Show Gist options
  • Save aauren/4157632 to your computer and use it in GitHub Desktop.
Save aauren/4157632 to your computer and use it in GitHub Desktop.
PowerShell Grep Equivilent
function Select-StringSpecial {
param(
[string] $query
)
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
$args = @{}
$tempArg = ""
foreach($param in $query.split(" "))
{
if("" -ne $tempArg)
{
if(-1 -ne $param.IndexOf(","))
{
$param = $param.Split(",")
}
$args.Add($tempArg, $param)
$tempArg = ""
continue
}
if(-1 -eq $param.IndexOf("-"))
{
$args.Add("pattern",$param)
}
else
{
$tempArg = $param
}
}
($_ | Out-String).split("`n") | Select-String @args
}
End {
# Executes once after last pipeline object is processed
}
}
Set-Alias ss Select-StringSpecial
Set-Alias grep Select-StringSpecial
@eizedev
Copy link

eizedev commented Jan 1, 2023

As a fast alternative for linux/unix grep (for files) you could also try the psitems powershell module. It includes a psgrep command (alias for Find-ItemContent) that works similar to the linux/unix grep command (the basic way, only a few parameters of original grep included).

Install-Module -Name PSItems
Import-Module -Name PSItems
psgrep 'test' -H -R 

(above command searches for pattern test in all files in current directory recursively (-R) and highlights the results (-H))

Fore more information check the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment