Why Monitor Disk Space?
Running out of disk space can crash services and databases. Automate monitoring to get early warnings before problems occur.
The Monitoring Script
# Set alert threshold (percent)
$Threshold = 15
# Get all local drives
$Drives = Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Used -gt 0}
$Alerts = @()
foreach ($Drive in $Drives) {
    $PercentFree = [math]::Round(($Drive.Free / ($Drive.Used + $Drive.Free)) * 100, 2)
    
    if ($PercentFree -lt $Threshold) {
        $Alerts += [PSCustomObject]@{
            Drive = $Drive.Name
            TotalGB = [math]::Round(($Drive.Used + $Drive.Free) / 1GB, 2)
            FreeGB = [math]::Round($Drive.Free / 1GB, 2)
            PercentFree = $PercentFree
        }
    }
}
# Send email if any drives are low
if ($Alerts.Count -gt 0) {
    $Body = $Alerts | ConvertTo-Html -Property Drive, TotalGB, FreeGB, PercentFree -PreContent "<h1>Low Disk Space Alert!</h1><p>Server: $env:COMPUTERNAME</p>" | Out-String
    
    Send-MailMessage -From "alerts@company.com" -To "admin@company.com" -Subject "ALERT: Low Disk Space on $env:COMPUTERNAME" -Body $Body -BodyAsHtml -SmtpServer "smtp.office365.com"
}
Schedule the Script
Run this daily using Task Scheduler:
- Create scheduled task
 - Trigger: Daily at 6:00 AM
 - Action: 
powershell.exe -File "C:\Scripts\DiskMonitor.ps1" 
Monitor Multiple Servers
Check remote servers:
$Servers = "Server01", "Server02", "Server03"
foreach ($Server in $Servers) {
    $Drives = Invoke-Command -ComputerName $Server -ScriptBlock {
        Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Used -gt 0}
    }
    # Process drives...
}            
💬 Comments (0)
💬 Join the conversation!
Login or create a free account to comment and get IT tips delivered to your inbox.