Home / Guides / Email when a Windows disk is low on space
Get an email when a Windows disk is running low on space
A full system drive stops a database, a log, a backup or an update, usually at night. Windows has no switch that mails you when free space falls below a limit, but PowerShell and Task Scheduler can do it in about forty lines. This guide gives you a script that checks every local drive against a percentage or a number of gigabytes, mails you once when a drive goes low and once when it recovers, how to schedule it, how to check other servers, and where this approach stops. We ran the script on a Windows 11 test machine against a small test drive that we filled and emptied.
Short answer.
Get-Volume returns Size and SizeRemaining for every volume. A script that compares them with a limit,
remembers which drives it has already reported, and runs every 15 minutes from a scheduled task
(schtasks /create /sc MINUTE /mo 15 /ru SYSTEM) gives you one mail when a drive drops below the limit and one when it has
recovered, instead of a mail every 15 minutes.
Send the mail through an SMTP relay you control: Send-MailMessage still works but Microsoft marks it obsolete because it cannot
guarantee a secure connection.
Read the free space
This lists every fixed drive that has a letter, with free space in GB and as a percentage:
Get-Volume | Where-Object { $_.DriveType -eq 'Fixed' -and $_.DriveLetter } |
Select-Object DriveLetter, FileSystemLabel,
@{ n = 'SizeGB'; e = { [math]::Round($_.Size / 1GB, 1) } },
@{ n = 'FreeGB'; e = { [math]::Round($_.SizeRemaining / 1GB, 1) } },
@{ n = 'FreePct'; e = { [math]::Round($_.SizeRemaining * 100 / $_.Size, 1) } } |
Sort-Object FreePct | Format-Table -AutoSize
Get-Volume is in the Storage module. Microsoft's page notes that dynamic volumes are only supported by some of the Storage
cmdlets, so check a server that uses them before you rely on this. A volume mounted in a folder has no drive letter and is skipped by the
script below; the DriveType test also keeps CD drives, removable drives and network drives out.
The script: one mail when a drive goes low, one when it recovers
The script compares each drive's free space with -MinFreePercent (default 10) and, if you set it, -MinFreeGB. A
drive is low when it is below either limit, which matters because 10 percent of a 100 GB system drive is a lot less than 10 percent
of a 4 TB data drive: a percentage alone alerts too late on big drives and too early on small ones. The script keeps the drives it has
reported in disk-state.json next to itself, so a drive that stays low sends one mail, not one per run. It treats the drive as
recovered only when it has 5 percent more headroom than the limit, so a drive sitting right at the limit does not send a mail each way every
run. -OutFolder writes the message to a text file instead of sending it, which is how we tested it without a mail server.
<#
Send-DiskAlert.ps1 - email when a local drive is running low on space, and once more when it has recovered.
Run it every 15 minutes from Task Scheduler. -OutFolder writes the message to a text file instead of sending it, for testing.
#>
param(
[double]$MinFreePercent = 10, # alert when free space is below this percentage of the drive...
[double]$MinFreeGB = 0, # ...or below this many GB (0 = ignore GB)
[string[]]$Drive = @(), # drive letters to check, e.g. C,D (empty = every fixed drive)
[string]$SmtpServer = 'smtp.contoso.local', # your internal relay
[int]$Port = 25,
[switch]$UseSsl,
[string]$From = 'alerts@contoso.com',
[string[]]$To = @('ops@contoso.com'),
[string]$OutFolder = ''
)
$ErrorActionPreference = 'Stop'
$stateFile = Join-Path $PSScriptRoot 'disk-state.json'
$low = @{} # drive letter -> 'low' while an alert is open
if (Test-Path $stateFile) {
(Get-Content $stateFile -Raw | ConvertFrom-Json).PSObject.Properties | ForEach-Object { $low[$_.Name] = $_.Value }
}
$lines = [System.Collections.Generic.List[string]]::new()
$volumes = Get-Volume | Where-Object { $_.DriveType -eq 'Fixed' -and $_.DriveLetter -and $_.Size -gt 0 }
if ($Drive) { $volumes = $volumes | Where-Object { $Drive -contains [string]$_.DriveLetter } }
foreach ($v in $volumes) {
$letter = [string]$v.DriveLetter
$freeGB = $v.SizeRemaining / 1GB
$freePct = $v.SizeRemaining * 100 / $v.Size
$detail = '{0}: {1:N1} GB free of {2:N1} GB ({3:N1}%)' -f $letter, $freeGB, ($v.Size / 1GB), $freePct
# The alert is open until the drive has 5% more headroom than the limit, so a drive sitting right at it does not send mail every run
$factor = if ($low.ContainsKey($letter)) { 1.05 } else { 1.0 }
$isLow = ($freePct -lt $MinFreePercent * $factor) -or ($MinFreeGB -gt 0 -and $freeGB -lt $MinFreeGB * $factor)
if ($isLow -and -not $low.ContainsKey($letter)) {
$low[$letter] = 'low'
$lines.Add("LOW $detail")
}
elseif (-not $isLow -and $low.ContainsKey($letter)) {
$low.Remove($letter)
$lines.Add("OK again $detail")
}
}
$low | ConvertTo-Json | Set-Content -Path $stateFile -Encoding UTF8
if ($lines.Count -eq 0) { return }
$subject = "Disk space alert on $env:COMPUTERNAME"
$body = ($lines -join "`r`n")
if ($OutFolder) {
$file = Join-Path $OutFolder ('alert-{0:yyyyMMdd-HHmmss-fff}.txt' -f (Get-Date))
Set-Content -Path $file -Value "Subject: $subject`r`n`r`n$body" -Encoding UTF8
return
}
# Send-MailMessage is obsolete (it cannot guarantee a secure connection): use it only towards a relay you control.
Send-MailMessage -SmtpServer $SmtpServer -Port $Port -UseSsl:$UseSsl -From $From -To $To -Subject $subject -Body $body
Save it as C:\Scripts\Send-DiskAlert.ps1 and set your relay, sender and recipients in the param block. The account the
task runs as needs write access to that folder.
Run it every 15 minutes
From an elevated PowerShell prompt:
# Elevated PowerShell. Checks every fixed drive every 15 minutes: alert below 10 percent or below 50 GB free.
$run = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Send-DiskAlert.ps1" -MinFreePercent 10 -MinFreeGB 50'
schtasks.exe /create /tn "Disk Alert" /sc MINUTE /mo 15 /tr $run /ru SYSTEM /rl HIGHEST /f
# Test it without mail: write the message to a folder instead
New-Item -ItemType Directory -Force C:\Scripts\outbox | Out-Null
C:\Scripts\Send-DiskAlert.ps1 -MinFreePercent 99 -OutFolder C:\Scripts\outbox
Remove-Item C:\Scripts\disk-state.json # forget the test alert
# Remove it again
schtasks.exe /delete /tn "Disk Alert" /f
The task runs as SYSTEM, so no password is stored. Run it once by hand with -OutFolder first to check the text, and once without
it to check that the mail arrives. Fifteen minutes is a sensible interval for space: a drive that fills in less than that is usually a runaway
log or backup, and a 10 percent limit leaves room to react.
Tested on Windows 11 (build 26300) on a test VM with a 200 MB virtual drive. With plenty of free space the script sent nothing. After we filled
the drive to 2.4 percent free it wrote one LOW message, and a second run sent nothing more. At 10.3 percent free, inside the 5 percent
headroom, the alert stayed open and nothing was sent; at 42.7 percent it sent one "OK again" message. -MinFreeGB 150 flagged the
same drive as low, and a run without -Drive checked both drives on the machine. The scheduled task was created with the command
above, run once as SYSTEM with -OutFolder, finished with Last Result 0 and wrote its message; it was deleted afterwards.
Send-MailMessage itself was not run, and neither were other servers or mounted-folder volumes.
Sending the mail
Microsoft's reference page for Send-MailMessage warns that the cmdlet is obsolete because it "doesn't guarantee secure
connections to SMTP servers", and recommends not using it, while saying there is no immediate replacement in PowerShell. It still works. The
honest options:
- An internal relay you control. Keep the cmdlet, as the script does, towards an SMTP relay on your own network that accepts mail from your servers without a password, and add
-UseSslwhen it supports it. - Exchange Online. Microsoft points to
Send-MgUserMailin the Microsoft Graph PowerShell SDK. It needs an app registration with the Mail.Send permission and a certificate for unattended sign-in. - A chat webhook. If you already use Teams or Slack for alerts,
Invoke-RestMethodto an incoming webhook is often simpler than mail.
Never put a mail password in the script or in the task's command line: anyone who can read the file or the task can read it.
More than one server
Get-Volume accepts -CimSession, so one script on a management server can read the drives of others. Each server then
needs its own entry in the state file, a name in the message and remoting that works from the task's account. That is the point where most
people stop writing scripts: the list of servers, the exceptions (the archive drive that is meant to be 95 percent full) and the thresholds
per drive are now configuration you maintain.
Where this approach stops
- Up to 15 minutes late. A drive that fills faster than the interval is caught after the fact, or not at all if the server stops working first.
- A dead server sends nothing. If the server is down or off the network, no task runs. Something outside it has to notice.
- One limit for every drive. The script applies the same percentage and GB to all drives it checks. Different limits per drive mean several tasks with
-Driveor edits to the script. - Free space is not growth. A drive at 40 percent free that loses 5 percent an hour is in trouble hours before it crosses the limit. A forecast needs history the script does not keep.
- A failed send is easy to miss. If the relay is down, the task's Last Run Result shows the error, but nobody looks there at 2 a.m., and the state file already says the alert was sent.
- Local volumes with a letter only. Mounted folders and network shares are not checked.
Questions
How do I get an email when a disk is almost full?
Run a PowerShell script from Task Scheduler every 15 minutes that reads Get-Volume, compares SizeRemaining with a limit, and sends a mail through an SMTP relay when a drive falls below it. Remember which drives you already reported so you send one mail per incident, not one per run.
What is a good threshold for low disk space?
A percentage alone is a poor rule: 10 percent of a 100 GB drive is 10 GB, 10 percent of a 4 TB drive is 400 GB. Use a percentage and a number of GB together and alert on whichever is hit first, then adjust per drive for archive volumes that are meant to be nearly full.
Why is Send-MailMessage obsolete?
Microsoft's documentation says it does not guarantee secure connections to SMTP servers and recommends not using it, while noting there is no immediate replacement in PowerShell. It still works. Use it only towards an SMTP relay you control, or send through Microsoft Graph or a chat webhook.
Can one script check the disks of several servers?
Yes: Get-Volume has a -CimSession parameter, so a script on a management server can read other servers' volumes. You then have to keep the list of servers, per-drive limits and the remoting setup yourself, which is where a monitoring tool starts to pay off.
Is there a tool that does this without scripting?
NotifyMeWhen has a Disk space watch: pick a drive or folder, set a limit as a percentage or in GB, and it emails you (or posts to Teams or Slack) when free space falls below it and again when it recovers. It watches local volumes on the machine it runs on and is licensed per machine, with a free 7-day trial.
Related
- Get an email when a Windows service stops
- Find failed scheduled tasks on many servers
- Email a SQL Server query as Excel every Monday
- NotifyMeWhen
- All guides
Sources (Microsoft Learn): Get-Volume, schtasks create, Send-MailMessage (obsolete warning), Send-MgUserMail.