| 437-991-3573 | Data Engineering Services
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance

Powershell - Disk Space Alert
by BF (Principal Consultant; Architecture; Engineering)

SQL Job - Disk Space Low - Alert

# Replace "" with your mail server name
$smtp=new-object Net.Mail.SmtpClient("")

# Set thresholds (in gigabytes) for C: drive and for the remaining drives

# Replace settings below with your e-mails

# Get SQL Server hostname
$hostname=Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name

# Get all drives with free space less than a threshold. Exclude System Volumes
$Results = Get-WmiObject -Class Win32_Volume -Filter "SystemVolume='False' AND DriveType=3"|
Where-Object {($_.FreeSpace/1GB –lt $driveCthreshold –and $_.DriveLetter -eq "C:")`
–or ($_.FreeSpace/1GB –lt $threshold –and $_.DriveLetter -ne "C:" )}

ForEach ($Result In $Results)
$drive = $Result.DriveLetter
$space = $Result.FreeSpace
$thresh = if($drive -eq 'C:'){$driveCthreshold} else {$threshold}

# Send e-mail if the free space is less than threshold parameter
# E-mail subject
"Disk $drive on $hostname has less than $thresh GB of free space left ",
# E-mail body
("{0:N0}" -f [math]::truncate($space/1MB))+" MB")