Search code examples
powershellsyntaxscriptingnotation

What does this syntax do: | select @{E={$_.PSComputerName}; L='Server'}, Name, ProcessId, PathName?


I have a Powershell script where there's a line:

$MyServers | select @{E={$_.PSComputerName}; L='AX Server'}, Name, ProcessId, PathName

What is the significance/function of this part? What does it do? Where can I understand the notation?

@{E={$_.PSComputerName}; L='AX Server'}


Solution

  • While Viet's answer is not technically wrong, it doesn't really explain why that hashtable is being created as a parameter of the Select-Object.

    It's generally referred to as a calculated property and it's used to customize the output of the object(s) you're selecting from. In your case, it's basically being used to rename an output column from PSComputerName to AX Server.

    The L key of the hashtable is short for Label and the E key is short for Expression. So it is equivalent to writing this:

    @{ Label='AX Server'; Expression={$_.PSComputerName} }
    

    In other words, output a column called AX Server with a value that equals the PSComputerName property of the current object in the list ($_).

    Calculated properties are often used to manipulate the data instead of just renaming columns. So you can do things like change the PSComputerName to upper case, do math on numeric values, or even combine multiple columns into one.