Shows size, free space and percent free for each fixed drive, and flags any below a threshold. Use it for a quick health check or as a scheduled task that exits with code 2 when space is low.
Tested
The script
<#
.SYNOPSIS
Reports free space on each fixed drive or volume.
.DESCRIPTION
Uses the .NET DriveInfo class, so it works on Windows, Linux and macOS
without extra modules. Drives below -WarnBelowPercent are flagged, and the
script exits with code 2 so a scheduler or monitoring tool can react.
Read-only.
.PARAMETER WarnBelowPercent
Flag drives with less free space than this percentage. Defaults to 15.
.EXAMPLE
./Get-DriveFreeSpace.ps1
.EXAMPLE
./Get-DriveFreeSpace.ps1 -WarnBelowPercent 25
#>
[CmdletBinding()]
param(
[ValidateRange(0, 100)]
[int]$WarnBelowPercent = 15
)
$drives = [System.IO.DriveInfo]::GetDrives() |
Where-Object { $_.IsReady -and $_.DriveType -eq 'Fixed' -and $_.TotalSize -gt 0 }
if (-not $drives) {
Write-Error 'No fixed drives found.'
exit 1
}
$report = foreach ($drive in $drives) {
$freePercent = [math]::Round(($drive.AvailableFreeSpace / $drive.TotalSize) * 100, 1)
[PSCustomObject]@{
Drive = $drive.Name
Label = $drive.VolumeLabel
SizeGB = [math]::Round($drive.TotalSize / 1GB, 1)
FreeGB = [math]::Round($drive.AvailableFreeSpace / 1GB, 1)
FreePercent = $freePercent
Status = if ($freePercent -lt $WarnBelowPercent) { 'LOW' } else { 'OK' }
}
}
$report | Sort-Object -Property FreePercent | Format-Table -AutoSize
$low = @($report | Where-Object { $_.Status -eq 'LOW' })
if ($low.Count -gt 0) {
Write-Warning "$($low.Count) drive(s) below $WarnBelowPercent% free."
exit 2
}
exit 0
-WarnBelowPercent sets the level that counts as low. It defaults to 15 and must be between 0 and 100.
Find the drives
[System.IO.DriveInfo]::GetDrives() is a .NET call that works on every platform PowerShell runs on. Where-Object keeps only drives that are ready, fixed (not DVD or network) and have a size, using -and to combine the tests.
Build the report
A foreach loop works out the percent free for each drive and builds a [PSCustomObject] with tidy columns. An if inside the object sets Status to LOW or OK by comparing with -lt.
Show it and set the exit code
The report is sorted with the emptiest drive first. If any drive is low, the script prints a warning and exits with code 2. Exit code 0 means everything is fine and 1 means no drives were found. A monitoring tool or scheduled task can act on those codes without reading the text.
On Linux
Each mount point shows up as its own drive, so bind mounts of the same disk appear more than once with identical numbers. The label column just repeats the mount point.
Cookies
No adverts, and nothing that follows you to another site. What we set is what signing in
and the forum need. We count page views with Umami, which sets no cookie and identifies nobody. The one exception is Google reCAPTCHA, which checks our forms are being used by
a person, and that one is yours to decline — nothing is loaded from Google until you
say so.
No adverts, and nothing that follows you to another site. We count page views without a cookie and identify nobody. Google reCAPTCHA checks our forms, and that one is yours to decline.
Cookie Policy ·
Privacy Policy