PowerShell: free and used space on logical disks
PowerShell commands to display the free and used space of Windows logical drives with Get-CimInstance, and to monitor these values.
The following commands use Get-CimInstance with the Win32_LogicalDisk class to display the free and used space, in GB, of each Windows logical drive. Values are converted to integers with [int].
Display all logical drives
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }},@{'Name' = 'UsedSpace (GB)'; Expression= { [int]($_.Size / 1GB - $_.FreeSpace / 1GB) }}
Filter on specific drives
Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DeviceID -in 'F:', 'H:' } | Select-Object -Property DeviceID, @{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }}, @{'Name' = 'UsedSpace (GB)'; Expression= { [int](($_.Size - $_.FreeSpace) / 1GB) }}
Monitor the space every 30 seconds
The following loop shows the status of drives F: and H: every 30 seconds. Stop the script with Ctrl+C.
while ($true) {
Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object { $_.DeviceID -in 'F:', 'H:' } | Select-Object -Property DeviceID, @{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }}, @{'Name' = 'UsedSpace (GB)'; Expression= { [int](($_.Size - $_.FreeSpace) / 1GB) }}
Start-Sleep -Seconds 30
}