A grep command (grepp
) in Powershell that lets you grep for a string in the text that would usually be printed in the terminal (not some other representation of the data). Output is a string, not a pipeline anymore - thus destructive.
To quickly grep through shell output without having to do where-object
gymnastics.
put this i.e. in your $profile
file.
function grepp {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[Object]$InputObject,
[Parameter(Position = 0)]
[string]$GrepString
)
begin {
$objects = [System.Collections.Generic.List[object]]::new()
}
process {
$objects.Add($InputObject)
}
end {
($objects | Out-String) -split "`n" | select-string $GrepString
}
}
(special thanks to @chrisdent
from powershell slack for helping to figure this out)
to quickly find out what aliases exist for where-object
you'd usually do something like:
PS> get-alias | Where-Object Displayname -like *where-object*
This requires you to
- find out what property to filter on (i.e. with something like
get-alias | select -property * -first 1
and/orget-alias | get member
, etc). - figure out what operator you need and write an expression for it (hint:
-contains where-object
doesn't work)
with grepp
this becomes less thinking and muscle memory instead:
PS> get-alias | grepp where-object
Alias ? -> Where-Object
Alias where -> Where-Object
PS> Get-Process | Select-Object -Property CommandLine -ExpandProperty CommandLine | grepp windows | grepp system32
C:\Windows\system32\DllHost.exe /Processid:{AB8902B4-09CA-4BB6-B78D-A8F59079A8D5}
"C:\Windows\system32\notepad.exe" C:\Users\user\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
C:\Windows\System32\RuntimeBroker.exe -Embedding
...
PS> & { write-host helloo; Write-Warning oops } *>&1 | grepp oo
helloo
oops