Steven's Notebook

Look Ma - No Hands!

grep, in PowerShell

Here is a sample of a PowerShell script that I use for finding text in files, having “grown up” with a more Unix-like syntax. I know, this isn’t exactly a clone of grep‘s functionality, but it gets me closer than having to remember exactly how to wrangle PowerShell’s Select-String commands to my liking. Note that I normally am looking for things recursively, so my script does that automatically.

Param(
  [string]$filename,
  [string]$target
)
ls -r $filename | sls $target

I call that by using an alias, set in my $PROFILE

Set-Alias grep c:\code\ps\Grep.ps1

Then I can just use a command like one of these

c:\> grep \code\*.ps1 version
c:\> grep *.txt hobbits
c:\> grep $HOME alpharetta

Also available, with any changes since this was published, in this repo.

Steven's Notebook © 2000-2018 Frontier Theme