Courses/PowerShell/Working with Objects

    Working with Objects

    PowerShell's object pipeline โ€” select, filter, sort, and format structured data.

    What You'll Learn

    • How PowerShell pipes objects (not text) between commands
    • Select-Object, Where-Object, Sort-Object, and ForEach-Object
    • Discovering properties/methods with Get-Member
    • Formatting and exporting data (CSV, JSON, XML)

    The Object Pipeline

    This is PowerShell's superpower. When you pipe commands together with |, you're not passing raw text โ€” you're passing .NET objects with properties and methods. This means you can filter by any property, sort by any field, and select exactly what you need.

    ๐ŸŽฏ Real-World Analogy: Bash pipelines are like passing notes written on paper โ€” you have to parse the text yourself. PowerShell pipelines are like passing spreadsheets โ€” columns, types, and sorting are built in.

    # The Big Three pipeline cmdlets:
    Get-Process | Where-Object { $_.CPU -gt 10 }   # FILTER
    Get-Process | Sort-Object CPU -Descending       # SORT
    Get-Process | Select-Object Name, CPU, Id       # PROJECT
    
    # Chain them for powerful one-liners:
    Get-Process | Where-Object CPU -gt 10 | Sort-Object CPU -Desc | Select-Object -First 5

    The $_ variable represents the current object in the pipeline (like "this item" in a loop).

    Object Pipeline

    See how objects flow through Select, Where, and Sort.

    Try it Yourself ยป
    JavaScript
    // PowerShell Object Pipeline โ€” simulated in JavaScript
    console.log("=== The Object Pipeline ===");
    console.log();
    console.log("In bash, pipes pass TEXT between commands.");
    console.log("In PowerShell, pipes pass OBJECTS with properties & methods.");
    console.log();
    
    // Simulate Get-Process output
    const processes = [
      { Name: "chrome",   CPU: 45.2, Memory: 512000, Id: 1234 },
      { Name: "code",     CPU: 23.1, Memory: 384000, Id: 2345 },
      { Name: "node",     CPU: 12.8, Memory: 256000, Id: 3456 },
    ...

    Exploring & Formatting Objects

    Use Get-Member (alias gm) to discover what properties and methods an object has:

    "Hello" | Get-Member          # String methods: ToUpper, Split, Replace...
    Get-Date | Get-Member          # DateTime properties: Year, Month, DayOfWeek...
    Get-Process | Get-Member       # Process: Name, CPU, Id, WorkingSet...

    PowerShell has four main format cmdlets for controlling output display:

    • Format-Table (ft) โ€” Columnar table view
    • Format-List (fl) โ€” Vertical key:value list
    • Format-Wide (fw) โ€” Single property in wide columns
    • Out-GridView โ€” Interactive GUI table (Windows)

    โš ๏ธ Common Mistake

    Never pipe Format-* output into further processing. Format cmdlets produce display objects, not data. Always put Format-* or Out-* as the last command in your pipeline.

    Members & Formatting

    Discover object members and format output.

    Try it Yourself ยป
    JavaScript
    // Formatting & Members โ€” simulated in JavaScript
    console.log("=== Exploring Object Members ===");
    console.log();
    console.log("Every object has properties and methods.");
    console.log("Use Get-Member to discover them:");
    console.log();
    console.log('PS> "Hello" | Get-Member');
    console.log("   TypeName: System.String");
    console.log("   Methods:  Contains, EndsWith, IndexOf, Replace,");
    console.log("             Split, StartsWith, Substring, ToLower,");
    console.log("             ToUpper, Trim, PadLe
    ...

    ๐Ÿ’ก Pro Tip

    Use Measure-Object for quick calculations: Get-Process | Measure-Object CPU -Sum -Average -Maximum gives you totals and stats in one go. Combine with Group-Object for powerful data analysis right from the shell.

    ๐Ÿ“‹ Quick Reference

    CmdletAliasPurpose
    Where-Objectwhere / ?Filter objects
    Select-ObjectselectPick properties / first N
    Sort-ObjectsortSort by property
    ForEach-Objectforeach / %Process each item
    Get-MembergmDiscover properties/methods
    Measure-ObjectmeasureCount, sum, average

    ๐ŸŽ‰ Lesson Complete!

    You now understand PowerShell's object pipeline โ€” the feature that makes it uniquely powerful. Next up: writing reusable functions and scripts.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service