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

Greg Booth 1,371 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 for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    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

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.