← script library

Monitor Disk Space Usage with PowerShell

Monitoring & HealthDecember 24, 2024

In this post, we will provide a PowerShell script that allows you to monitor disk space usage across your system. Keeping track of available disk space is crucial for preventing unexpected issues due to insufficient storage. This script checks the current usage of all drives and presents a concise report, allowing administrators to identify drives that may require attention. Here is the PowerShell script for monitoring disk space usage:

powershell
# Get the disk space information for all drives
$drives = Get-PSDrive -PSProvider FileSystem
# Prepare the report
$diskSpaceReport = @()
foreach ($drive in $drives) {
    $diskSpaceReport += [PSCustomObject]@{
        Drive        = $drive.Name
        UsedSpaceGB  = [math]::round($drive.Used / 1GB, 2)
        FreeSpaceGB  = [math]::round($drive.Free / 1GB, 2)
        TotalSpaceGB = [math]::round($drive.Used / 1GB + $drive.Free / 1GB, 2)
        PercentFree  = [math]::round(($drive.Free / ($drive.Used + $drive.Free)) * 100, 2)
    }
}
# Output the disk space report
$diskSpaceReport | Format-Table -AutoSize
Write-Host "Disk space monitoring completed successfully."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.