Display DiskSize

Welcome Forums General PowerShell Q&A Display DiskSize

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5160

    Points: 0
    Rank: Member

    by johnvanleeuwen at 2012-10-01 23:27:30


    I'm banging my head against the wall on something. I want to get info on my diskdrives. I have the info I like but a little to much. This is the powershell I use.

    Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" | Select DeviceID,VolumeName,@{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

    Works like a charm, but I want this "script" only to show me a couple of drives. I don't need to see the "System Reserved" drive or attached network drives. Any idea's on how to get only the info on, for example; C, D and E drive? Is this possible in a oneliner?

    Hope you all can help me out!

    by surveyor at 2012-10-02 03:44:51

    Hi John,
    you can go two easy ways for example:

    First you can give your filtercriteria directly to the WMI like this:
    Get-WmiObject -Class Win32_LogicalDisk -filter "DriveType=3 and DeviceID like '[DEF]:'" |
    Select-Object -Property `
    @{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
    @{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

    And second you can filter after the WMI has given back all the data:
    Get-WmiObject -Class Win32_LogicalDisk -filter "DriveType=3" |
    Where-Object { $_.DeviceID -like '[DEF]:' } |
    Select-Object -Property `
    @{Name="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
    @{Name="freespace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

    [DEF]: stands for D: or E: or F:

    I would prefer the first solution because it's better to only call for data you need than filter it out later.

    PS: I have wrapped the line for better reading.

    by RichardSiddaway at 2012-10-02 05:11:36

    Filter as soon as possible – especially when retrieving data across the network. You could use the -Property parameter of Get-WmiObject to perform your property filtering

    by johnvanleeuwen at 2012-10-05 03:25:47

    Thanks! This is working! Thanks for the explanation I get it now!

The topic ‘Display DiskSize’ is closed to new replies.