Powershell equivalent to Grep?

ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
Hi guys

take the following command:

get-process get-help full | more

Can I take this one step further and perform some grep equivalent wizardry and search for specific text within the help file? if so then please hit me up an answer.

I have tried but failed as I am getting no output

get-help get-process -full | Select-String -Pattern "auto" -CaseSensitive


Powershell wizards enlighten me :)
Microsoft's strategy to conquer the I.T industry

" Embrace, evolve, extinguish "

Comments

  • NetworkNewbNetworkNewb Member Posts: 3,298 ■■■■■■■■■□
    What exactly are you looking to find? I dont think anything in the processes section under help will say "auto"


    edit: If you're looking for services that are "auto" it would be...

    Get-wmiobject win32_service | where-object {$_.Startmode -eq 'Auto'} | ft
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    I was just experimenting really, I am new to powershell within the help file it says Related Links

    on Linux you could for example just run cat log.txt | grep related

    and it would output any strings containing related within the file. Is something similar available on Powershell or am I being a idiot here i.e outputting a single string from a help file.
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • jelevatedjelevated Member Posts: 139
    I don't know the exact syntax here, maybe someone else can chime in. I can take a look later if no one provides the answer.

    Basically what you are looking at when you run the get-help function, its actually compiled on the fly and output to the console. Select-String will work on text/strings, but the get-help function isn't actually returning a string for you to process. It's printing the various details about the function but that doesn't make it in a format that Select-String likes, cause its not a string.

    You'll need to save the console output to a variable and then work on it somehow. There may be some fancy one liners available.
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Thank you for the explanation dude, I thought it was just essentially like a man page where you can grep whatever you want.
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • NetworkNewbNetworkNewb Member Posts: 3,298 ■■■■■■■■■□
    ahhh gotcha :) Yea

    Select-String -Pattern 'word' .\textfilenamehere.txt
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    so apply the above to my original thought process how would the correct command work to output a item from the get-help file? or am I not understanding things correctly.

    get-help get-process -full | Select-String -Pattern "auto"
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • NetworkNewbNetworkNewb Member Posts: 3,298 ■■■■■■■■■□
    I see, not 100% sure but this would be the closest thing I could think of.

    get-help | out-string -stream | select-string "auto"

    edit: was bored, this is the closest I think you can easily get...Not exactly what you wanted but sorta close, sorta

    get-help * -full | format-list | out-string -stream | select-string "auto"
  • blargoeblargoe Member Posts: 4,174 ■■■■■■■■■□
    It really isn't that straight forward. Get-Help, for example returns an object with members that are themselves, objects (ie, the examples, description, parameters, etc are all properties that are different object types that are not string). You can see this by piping the get-help to get-member.

    I don't know of a way off hand to take the text output of these objects that are returned by get-help and search by a string pattern without doing something like looping through the examples returned by -Full and searching from there. There may be a way, but I haven't encountered a need to do this so I am not sure.

    For a text file, it is pretty easy as noted by someone else above.
    IT guy since 12/00

    Recent: 11/2019 - RHCSA (RHEL 7); 2/2019 - Updated VCP to 6.5 (just a few days before VMware discontinued the re-cert policy...)
    Working on: RHCE/Ansible
    Future: Probably continued Red Hat Immersion, Possibly VCAP Design, or maybe a completely different path. Depends on job demands...
  • QordQord Member Posts: 632 ■■■■□□□□□□
    The get-help results count as objects, so one option is to convert the output to a series of strings using the -stream parameter to do what you want. This should do what you're looking for, I used "false" because "auto" isn't found in that man page:
    Get-Help get-process -Full | Out-String -Stream | Select-String -Pattern false
    
    The output isn't really helpful for anything though, as-is it's really just gibberish:
    Required?                    false
    Dynamic?                     false
    Required?                    false
    Accept pipeline input?       false
    Dynamic?                     false
    Dynamic?                     false
    Accept pipeline input?       false
    Dynamic?                     false
    Dynamic?                     false
    Required?                    false
    Accept pipeline input?       false
    Dynamic?                     false
    Required?                    false
    Dynamic?                     false
    

    But if you add the -context param to select-string you can also see, for example, the 3 lines above and below of the found result:
    Get-Help get-process -Full | Out-String -Stream | Select-String -context 3 -Pattern false
      PARAMETERS
          -ComputerName <string[]>
    
    
    >         Required?                    false
              Position?                    Named
              Accept pipeline input?       true (ByPropertyName)
              Parameter set name           Id, Name, InputObject
              Aliases                      Cn
    >         Dynamic?                     false
    
    This way we get some info about the string and where it lives, and not just the string itself. Alternatively, you could use -context 3,0 to get the string and the three lines before it, or 0,3 for the string and three after it.
Sign In or Register to comment.