メインコンテンツへスキップ
見出し画像

[Powershell]get-alias | select Definition, DisplayName | where {$_.Definition -like "*Object"} | ForEach-Object {($_.DisplayName -split "->")[1]}

    PS C:\Users\sakai> get-alias | select Definition, DisplayName | where {$_.Definition -like "*Object"} | ForEach-Object {($_.DisplayName -split "->")[1]}
     ForEach-Object
     Where-Object
     Compare-Object
     Compare-Object
     ForEach-Object
     Group-Object
     Get-WmiObject
     Measure-Object
     Remove-WmiObject
     Select-Object
     Sort-Object
     Tee-Object
     Where-Object

     ## Decoded by ChatGPT

    Sure 🙂
    Here is a clear, natural English translation, written for learners and PowerShell users.


    Explanation of the PowerShell Script

    (with a focus on Where-Object, Select-Object, and ForEach-Object)

    This is a very good example.
    A single pipeline line contains almost all of PowerShell’s core ideas.
    I will explain it step by step, with special attention to Where-Object, Select-Object, and ForEach-Object.


    1. Overview — What does this script do?

    get-alias |
    select Definition, DisplayName |
    where {$_.Definition -like "*Object"} |
    ForEach-Object {($_.DisplayName -split "->")[1]}
    

    👉 This pipeline:

    • Lists aliases whose original cmdlet name ends with Object

    • Extracts only the original cmdlet names (not the alias names)

    The second command does the same thing, but by using [0], it extracts the alias names instead.


    2. PowerShell Pipeline Philosophy (Very Important)

    In PowerShell:

    • The pipe (|) passes objects, not text

    • Each command receives objects and outputs objects

    Conceptually:

    Object → Object → Object → Object
    

    This is the foundation for understanding everything that follows.


    3. Get-Alias — Input Objects

    get-alias
    

    This command outputs many AliasInfo objects.

    Example (one item):

    DisplayName : % -> ForEach-Object
    Definition  : ForEach-Object
    

    Meaning:

    PropertyDescriptionDefinitionThe original cmdlet nameDisplayNameDisplay string: alias -> original


    4. Select-Object — Shaping the Data

    select Definition, DisplayName
    

    Purpose

    👉 Keeps only the specified properties (projection)

    • Input: AliasInfo objects (many properties)

    • Output: Objects containing only Definition and DisplayName

    Key point:

    • Select-Object does not filter rows

    • It reduces columns, not items


    5. Where-Object — Filtering by Condition

    where {$_.Definition -like "*Object"}
    

    Core Idea of Where-Object

    👉 A filter that decides whether each object passes through

    • Objects are evaluated one by one

    • True → object continues

    • False → object is discarded

    What is $_?

    $_
    

    👉 The current object coming through the pipeline.

    In this script:

    $_.Definition
    

    represents values such as:

    ForEach-Object
    Where-Object
    Select-Object
    ...
    

    Meaning of the Condition

    -like "*Object"
    
    • Wildcard comparison

    • *Object means “ends with Object”

    So:

    Keep only aliases whose Definition ends with Object


    6. ForEach-Object — Processing Each Object

    ForEach-Object {($_.DisplayName -split "->")[1]}
    

    Core Idea of ForEach-Object

    👉 Executes processing on each object

    • Where-Object: decide pass or discard

    • ForEach-Object: transform or extract data

    Contents of DisplayName

    Example:

    % -> ForEach-Object
    

    Splitting the String

    $_.DisplayName -split "->"
    

    Result:

    [0] "% "
    [1] " ForEach-Object"
    

    Taking [1]

    ForEach-Object
    Where-Object
    Select-Object
    ...
    

    👉 This extracts the original cmdlet names.


    7. Difference in the Second Command

    ForEach-Object {($_.DisplayName -split "->")[0]}
    

    This produces:

    % 
    ? 
    compare 
    diff 
    foreach 
    ...
    

    👉 These are the alias names.


    8. Summary of the Three Cmdlets

    CmdletRoleMental ModelSelect-ObjectChoose propertiesShape the tableWhere-ObjectFilter objectsConditional filterForEach-ObjectProcess objectsMap / transform


    9. Common Misunderstandings

    ❌ “ForEach-Object is just a loop”

    → Half true, half wrong

    • Conceptually, it is a transformation operator

    • Its output flows to the next pipeline command

    ❌ “Where and Select can be swapped freely”

    → Incorrect

    select → where
    

    means:

    • Reduce properties first, then filter rows

    where → select
    

    means:

    • Filter rows first, then reduce properties

    👉 Meaning and performance may differ.


    10. Key Takeaways

    • PowerShell uses an object-oriented pipeline

    • Select-Object: shape data

    • Where-Object: filter data

    • ForEach-Object: transform data

    • $_ represents the current pipeline object

    If you fully understand this single pipeline,
    you have already reached the entry level of intermediate PowerShell usage 👍

    Good next steps to explore:

    • Select-Object -ExpandProperty

    • Short-form syntax of Where-Object

    • Difference between ForEach-Object and the foreach language keyword

    If you want, I can also rewrite this as a formal technical article or a beginner-friendly tutorial.



    あなたへのおすすめ