grep for Windows

February 17, 2015

Today I wanted to consolidate tags and categories on this blog. Every post is just a plain text file, so on unix the tool I’d reach for is grep. Select-String is a good choice for this task in PowerShell.

Objective:

  1. Get all articles
  2. Show only the line with the word “Category” on it

Just like in unix, the pipe operator makes it easy to compose functionality.

Get-ChildItem -File -Path ".\content" | Select-String "Category"

Full cmdlet names are great for scripts, but in the shell it’s usually a little faster to just go with the aliases.

ls -File -Path ".\content" | sls "Category"

Finally, sometimes it’s really important to only match the correct case.

ls -File -Path ".\content" | sls "Category" -CaseSensitive

As always, for more:

Get-Help Select-String -Examples

Profile picture

Written by @sghill, who works on build, automated change, and continuous integration systems.