Monitor Disk Space with PowerShell for Efficient Management

Welcome to today’s feature on enhancing IT operations through automation. We bring you a PowerShell script to monitor disk space usage on your servers, helping prevent downtime by proactively managing resources. Efficiently maintaining disk space is crucial to ensure your systems run smoothly without interruptions.
This script pairs excellently with our ServerEngine software, which is designed to bolster your server automation strategies. By integrating ServerEngine with PowerShell scripts like this one, you can further streamline your server management tasks and focus on critical business functions.
Dive into the script below and see how you can ensure optimal performance across your server environments:

# PowerShell script to monitor disk space and send alerts for low space
# Define threshold for low disk space in GB
$lowSpaceThreshold = 20
# Get all logical disks
$disks = Get-PSDrive -PSProvider FileSystem
# Function to check disk space
foreach ($disk in $disks) {
    # Calculate free space in GB
    $freeSpaceGB = [math]::Round($disk.Free / 1GB, 2)
    if ($freeSpaceGB -lt $lowSpaceThreshold) {
        $message = "Warning: Disk [$($disk.Name)] on server [$env:COMPUTERNAME] has low free space: $freeSpaceGB GB"
        # Print alert message
        Write-Host $message -ForegroundColor Red
        # Add your custom notification action here, e.g., send an email or log the event
    } else {
        Write-Host "Disk [$($disk.Name)] has sufficient free space: $freeSpaceGB GB"
    }
}