Hacker News new | ask | show | jobs
by gecko 3903 days ago
I suspect you haven't read the documentation or tried using PowerShell for any length of time. PowerShell may be bash-like, but it's not bash, does not try to be bash, and won't reward you if you treat it like bash. But it's very orthogonal, clean, and well documented.

For your particular example:

    > help grep
    
    Name                              Category  Module                    Synopsis
    ----                              --------  ------                    --------
    Out-File                          Cmdlet    Microsoft.PowerShell.U... Sends output to a file.
    Select-String                     Cmdlet    Microsoft.PowerShell.U... Finds text in strings and files.
Okay, so Select-String sounds really promising. Let's take a look.

    > help select-string

    NAME
        Select-String

    SYNOPSIS
        Finds text in strings and files.


    SYNTAX
        Select-String [-Pattern] <String[]> [-Path] <String[]> [-AllMatches] [-CaseSensitive] [-Context
        [<Int32[]>]] [-Encoding {unicode | utf7 | utf8 | utf32 | ascii | bigendianunicode | default | oem}]
        [-Exclude [<String[]>]] [-Include [<String[]>]] [-InformationAction {SilentlyContinue | Stop | Continue |
        Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-List] [-NotMatch] [-Quiet]
        [-SimpleMatch] [<CommonParameters>]
Most of those command flags seem really straightfoward to me. Can you be more specific about what was unclear to you? I'd be happy to help.

EDIT: BTW, it's a bit annoying to type Select-String, so it'd be nice if it had an alias. Does it have one?

Well, you can get aliases in PowerShell by typing alias. But that'll give you a wall of text; what you want to do is to quickly search for things that are aliased to Select-String.

There are two ways to do this. First, you can pipe to a GUI that allows directly filtering the results:

    > alias | out-gridview
Or, alternatively, we can figure out what objects alias gives us:

    > alias | select -first 1 | get-member
Note that we've got a Definition field, and query on that:

    > alias | Where-Object { $_.definition -contains 'Select' }
which is a long version of

    > alias | ? { $_.definition -contains 'Select' }
And notice that Select-String is aliased to sls by default. Note that I can just reference the column by name, rather than going through some awk/cut fun.

You might be annoyed that Select-String is not aliased to grep. While many commands actually do have multiple aliases to both DOS and Unix equivalents (e.g., Get-ChildItem is aliased to gci, dir, and ls, and Remove-Item has ri, rm, and del as aliases), Select-String works differently enough from grep that it's not a default alias so you're not confused.

2 comments

Fair enough, and I have and will continue to not really want to invest time into learning Microsoft's stuff, based on past experiences (my first being wanting to develop for the platform as a scrub, and they charged thousands of dollars for a dev environment/compiler). I honestly don't think it'll serve me well going forward, when there are better alternatives.

They have had decades now to get on board with developers, and cmd.exe is still their go-to console. They still strike me as a company of greedy corporate business folk, who happen to run a software company, as opposed to a software company that has to suffer greedy corporate drones to survive in our current race to the bottom.

Powershell is the goto console. Cmd is legacy

In fact most admin gui's are now just calling powershell now.

Select-String works differently enough from grep

Correct - it has a couple of crippling features, such as wrapping output at 80 characters even if piped to a file (unless you tell it otherwise every time).