Avantgarde Technologies

<a href="http://www.avantgardetechnologies.com.au">Avantgarde Technologies</a>
Perth's IT Experts

Wednesday, September 15, 2010

How to GREP in Powershell

In Linux and Unix world when we want to grab a line that contains specific output we would just use the following command:

command | grep search

In powershell to do the same thing we use this command:

command | ls *search*

For example we run the following command to list a whole bunch of output for:


We want to to identify all lines that have the word "Name" in it. Run the following command:

Get-AcceptedDomain | fl *name*

Very easy.


  1. 'get-windowsfeatures | fl *framework*' results in a lot of blank lines.

    'get-windowsfeatures | ls *framework*' results in a lot of errors.

  2. Same here. Any idea how to get this working?

  3. Firstly, that's an EXCHANGE PowerShell, which has different plugins and modules available to it, but the command (Format-List) is in both.

    Some commands operate on Objects, and some operate on text. The Get-AcceptedDomain command outputs objects, which the Format-List or Get-Item (ls) command work on. Using it to parse text from the output doesn't work, but parsing objects should (or something like that).

  4. Not helpful. Perhaps you could give more detail/explanation. I came here visiting: http://social.msdn.microsoft.com/Forums/en/windowssdk/thread/592ea672-1c3d-40ce-8669-59148d3b6e3f

  5. Yeah, how do I do this in powershell?

    grep search_string dir/*/subdir/file.txt

  6. "Anonymous said...
    'get-windowsfeatures | fl *framework*' results in a lot of blank lines.

    'get-windowsfeatures | ls *framework*' results in a lot of errors."

    $r = get-windowsfeature

    $r | get-member

    and look at column Name (where MemberType is Property) idea, right?

    and, finally, we get, for example

    Get-WindowsFeature | Where-Object {$_.DisplayName -like '*active directory*'}

    hope this help,

  7. > Yeah, how do I do this in powershell?
    > grep search_string dir/*/subdir/file.txt

    What has worked for me this morning is:

    more dir/*/subdir/file.txt | select-string search_string

    I don't understand why it is so much less usable than the unix equivalent though.

  8. command | select-string -pattern "texttofind"

  9. less usable? its only different.

  10. hah, check out this powershell problem.
    echo blah > asdf.txtt
    Get-Content .\*.txtt | select-string blah > asdf2.txtt

    you'll see asdf.txtt is small, but asdf2.txtt never stops growing until you kill the command, as it feeds back into itself, unlike grep.

    1. and there seems to be several different ways, ls, fl, and now select-string, which may work or not. I don't need to know to use ls, fl, nor select-string when used in combination with this or that command, grep just works.

  11. how to convert this unix cmd (grep -v "^Enter" 123.imp > 321.imp) to powershell