Search code examples
powershellfilterwmiwqlcim

Retrieve specific drive data - Get-WmiObject Win32_logicaldisk


I am attempting to retrieve the FreeSpace for a specific drive using Get-WmiObject.

Using Get-WmiObject Win32_logicaldisk on my machine returns the following:

PS C:\Users\Julian\Desktop\Tools\Powershell> Get-WmiObject Win32_logicaldisk | Select-Object DeviceID, FreeSpace
DeviceID  FreeSpace
--------  ---------
C:  47114498048
S:  9963356160
Z:  985061974016

I want to specifically request the FreeSpace dependent on the drive letter (DeviceID) I specify however I'm unsure how to do so as I'm rather new to powershell. Any help would be appreciated.


Solution

  • Use the -Filter parameter to filter the objects at the source:

    PS> Get-WmiObject Win32_logicaldisk -Filter 'DeviceId = "C:"' | Select-Object DeviceID, FreeSpace
    
    DeviceID   FreeSpace
    --------   ---------
    C:       14188314624
    

    Filter expression 'DeviceId = "C:"' is essentially the WHERE clause of a WQL statement.


    Note, however, that the *-Cim-* cmdlets have superseded the *-Wmi* cmdlets in PSv3+.

    Fortunately, WQL is an implementation of CQL (the CIM Query Language), so the same filter can be used:

    PS> Get-CimInstance Win32_logicaldisk -Filter 'DeviceId = "C:"' | Select-Object DeviceID, FreeSpace
    
    DeviceID   FreeSpace
    --------   ---------
    C:       14188314624