Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

Do you remember what the common commands for pipelining you used were?

I feel like my PowerShell struggles are really about not knowing 2-5 core, general-purpose pipelining commands well enough to use in any situation.



Not OP but the "-Object" commands are pretty fundamental to creating useful pipelines:

Select-Object - Pick out specific fields from an object, create calculated fields etc.

Where-Object - Drop non-matching objects from the rest of the pipeline.

Group-Object - Cluster objects into groups based on a shared property value.

Sort-Object - Order an array of objects based on a property value.

Get-Content - Read from a file.

ConvertFrom-(CSV/JSON) - Parse a json/csv formatted string into a powershell object.

ConvertTo-(CSV/JSON) - Serialise a powershell object into a csv/json string.

(Parallel)ForEach-Object - Loop over each item in the pipeline, performing one or more actions on it. Usually occurs at the end of the pipeline, or when you need to call an executable that can't handle pipeline input.

One thing I struggled with in the beginning, was not knowing what properties an object might have. You can pipe any object into `Get-Member` and it'll list its available properties and methods.

Many of the "-Object" commands support the use of script blocks if you need to carry out more complex filtering/projection.


Thanks for the detailed write-up! I'll give them a try today.


    |? PropertyName -gt 5            # where-object filter numbers
    |? PropertyName -match 'text'    # where-object filter text
    |? {$_.thing ...}                # where-object filter script

    |% PropertyName    # same as select-object -expandproperty propertyname
    |% { $_.thing }    # foreach loop
 
    |sort
    |sort prop1, prop2
    |sort {$_.Name.Substring(0,5) -as [int]}   # sort calculated thing

    |fl *        # format-list , all properties
    |ft -auto    # format-table, autosize table column widths

    |sv q        # set-variable -name q, same as $q = <...>
    |tee -var q  # same but show the results as well as storing in q


Helpful! Thanks!


Others have provided better answers than I could have, as I don’t remember clearly enough.

What I do remember is that until I understood what I was trying to do and why (mostly error handling around edge cases, all specific to the app), I couldn’t really grok the various object management calls, but that once I’d done a few rounds of PoCing and RTFMing, everything fell into place.


Get-ChildItem, Select-Object, Where-Object, Sort-Object or anything that is operating on a collection feel very natural. Often times I'm piping Get-ChildItem into sort to find the most recently created file in a directory.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: