Configure Windows Script to Check Disk Space and Send Email Alerts

Summary

When your computer starts running slower, it could be due to low disk space. This guide shows you how to configure a Windows script to monitor your disk space and send email alerts when the available space is low, preventing system slowdowns.

Andrew Wright

Updated on 2024/11/02

by Andrew Wright

How to Use Windows Script to Check Disk Space and Send Email Alerts

Setting up a Windows script to check your disk space regularly can help you avoid issues like system slowdowns and crashes due to low storage. Here's a simple script that checks your disk space and sends an email alert when it's about to run out.

This solution involves creating a script that checks your hard disk space, and sends you an email notification when the disk usage exceeds a certain threshold (e.g., 90%). Follow the steps below:

Step 1: Create the Script

Create a script file with the following content. You can save it as check_disk_space.sh in your home directory:

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' youremail@example.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
    

Step 2: Automate the Script

To ensure that the script runs daily, you need to add it to your system's cron job scheduler. Open the /etc/crontab file and add the following line at the end:

@daily ~/check_disk_space.sh
    

Remember to replace youremail@example.com with your actual email address. The script is configured to send an alert if disk usage exceeds 90%. You can adjust this threshold if needed.

What to Do When You Receive a Low Disk Space Alert

If you receive a low disk space alert, it means your system drive (usually C:) is more than 90% full. Here are some actions you can take to free up space and prevent performance issues:

Clean Up Junk Files on the System Drive

The first step is to clean up unnecessary files that are taking up space. You can start by:

  • Deleting temporary files and cached data.
  • Uninstalling programs you no longer need.
  • Using Windows built-in tools like Disk Cleanup to remove system files that are no longer needed.

Extend the C Drive with System Built-in Tools

Note: Windows built-in tools can work if there is unallocated space adjacent to the partition you want to extend. If there is another partition or the recovery/oem partition stand in the way, try a third party free tool instead.

If your C drive is nearly full and there is unallocated space available, you can try extending your C drive partition using built-in Windows tools:

  • Go to My Computer > Manage > Disk Management.
  • Right-click on the C drive and select Extend Volume.

If you prefer using the command line, you can use the diskpart command:

  • Press Win + R, type CMD, and press Enter.
  • Type diskpart in the command prompt window.
  • Use the command list volume to see all your disk partitions.
  • Select the C drive with select volume X (where X is your C drive).
  • Extend the partition with extend size=10240 (10240 MB, for example when there is at least 1024MB adjacent unallocated space is available).

Note: Windows built-in tools can only extend c drive when it has NTFS file system and adjacent unallocated space is at its right side adjacent to it.

More requirements?

  • Free space, not unallocated space? No
  • FAT32 c drive? No
  • 2TB limit for MBR disk
  • The unallocated space must be on the same disk with the c drive.

Resize the Disk Partition Using Partition Management Freeware

Windows built-in tools can only extend partitions when adjacent unallocated space is available. If there is no unallocated space or if you encounter issues, consider using third-party partition management software.

We recommend using IM-Magic Partition Resizer Free, a powerful tool that allows you to resize, move, and merge partitions safely without data loss.

Video: How to resize partition without losing data

For Windows 11/10/8/7 => Download Partition Resizer Free Portable [100% Free]

For Windows Server 2022-2003 => Download Partition Resizer Server Portable [Free Demo]

Related Product