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:

Get-AcceptedDomain



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

Get-AcceptedDomain | fl *name*



Very easy.

13 comments:

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

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

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

    ReplyDelete
  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).

    ReplyDelete
  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

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

    grep search_string dir/*/subdir/file.txt

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

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

    try
    $r = get-windowsfeature

    then
    $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,
    wbr

    ReplyDelete
  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.

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

    ReplyDelete
  9. less usable? its only different.

    ReplyDelete
  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.

    ReplyDelete
    Replies
    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.

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

    ReplyDelete