This PowerShell script helps administrators monitor disk space usage on specified drives. By automating this task, the script ensures timely warnings about low disk space, helping to maintain system performance and prevent potential issues.
powershell
$drives = @('C:', 'D:', 'E:')
$threshold = 10 # Threshold in GB
foreach ($drive in $drives) {
$diskSpace = Get-PSDrive -Name $drive
$availableSpaceGB = [math]::round($diskSpace.Free / 1GB, 2)
if ($availableSpaceGB -le $threshold) {
Write-Host "Warning: Drive $drive has only $availableSpaceGB GB left."
} else {
Write-Host "Drive $drive has $availableSpaceGB GB available."
}
}
$logFilePath = "C:\DiskSpaceLog.txt"
$currentDateTime = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
foreach ($drive in $drives) {
$diskSpace = Get-PSDrive -Name $drive
$availableSpaceGB = [math]::round($diskSpace.Free / 1GB, 2)
$logMessage = "$currentDateTime - Drive $drive: $availableSpaceGB GB available"
Add-Content -Path $logFilePath -Value $logMessage
}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.
More in Monitoring & Health
Disk Space Cleanup and Report Script
2025-01-14User Account Status Reporting Script
2025-01-13Monitor System Performance with PowerShell
2025-01-12Automating System Health Check with PowerShell
2025-01-05Automate System Performance Monitoring with PowerShell
2024-12-30Enhancing System Monitoring with PowerShell: Real-time Log Management
2024-12-29Ready when you are.
Try ServerEngine free for 7 days.