Search code examples
powershell

PowerShell: how to grep command output?


In PowerShell I have tried:

alias | select-string Alias

This fails even though Alias is clearly in the output. I know this is because select-string is operating on some object and not the actual output string.

What can be done about it?


Solution

  • There are two problems. As in the question, select-string needs to operate on the output string, which can be had from "out-string". Also, select-string doesn't operate linewise on strings that are piped to it. Here is a generic solution

    (alias|out-string) -split "`n" | select-string Write