How do i change the header label when using Select-Object in powershell

Greg Booth 1,296 Reputation points
2022-11-07T11:17:34.59+00:00

I am trying to get a list of drives and the size. I do not want the header in the output to be $_.Size. I want the header to be Size2 (for the sake of argument) so am trying to use a label, expression hashtable as follows

Get-WmiObject -Class Win32_LogicalDisk -ComputerName myserver | ? {$. DriveType -eq 3} | Select-Object DeviceID,{$.Size},{label='Size2';expression={{$_.Size}}}

But this just gives a extra blank column called "label='Size2';expression={{$_.Size}}"
What am i doing wrong ?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,432 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 100.1K Reputation points MVP
    2022-11-07T11:26:07.203+00:00

    Hi @Greg Booth ,

    maybe this helps to get it going (this is getting the local drives):

    Get-WmiObject -Class Win32_LogicalDisk |  
        Where-Object {$_.DriveType -eq 3} |  
            Select-Object DeviceID, Size, @{label='Size2';expression={$_.Size}}  
    

    Result looks like this:

    257886-image.png

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


0 additional answers

Sort by: Most helpful