Ensuring that your Windows servers and workstations have sufficient disk space is critical to maintaining system performance and avoiding potential downtime. Running out of disk space can lead to a host of issues, including application failures, system crashes, and data loss. To help you stay on top of your disk space usage, we’ve put together a simple yet effective PowerShell script that monitors disk space on all partitions and sends an email alert when free space falls below a specified threshold.

PowerShell Script for Disk Space Monitoring

Below is a PowerShell script that checks the available disk space on all partitions and sends an email alert if the free space on any partition is below a defined threshold. This script can be scheduled to run at regular intervals using Windows Task Scheduler.

PoweShell Script

# Define the threshold in GB
$threshold = 10

# Get disk space information
$diskInfo = Get-PSDrive -PSProvider FileSystem

foreach ($disk in $diskInfo) {
    $freeSpaceGB = [math]::round($disk.Free / 1GB, 2)
    $usedSpaceGB = [math]::round($disk.Used / 1GB, 2)
    $totalSpaceGB = [math]::round($disk.Used / 1GB + $disk.Free / 1GB, 2)
    # Check if free space is below the threshold
    if ($freeSpaceGB -lt $threshold) {
        $message = "Warning: Drive $($disk.Name) on $($env:computername) has only $freeSpaceGB GB free space left, which is below the threshold of $threshold GB.`n"
        $message += "Total Space: $totalSpaceGB GB`n"
        $message += "Used Space: $usedSpaceGB GB`n"
        $message += "Free Space: $freeSpaceGB GB`n"
        # Send an alert (this can be modified to send an email or log the warning)
        Write-Output $message
        # Example to send an email alert (configure SMTP settings accordingly)
        $smtpServer = "smtp.yourserver.com"
        $smtpFrom = "alert@yourdomain.com"
        $smtpTo = @("admin1@yourdomain.com", "admin2@yourdomain.com")  # Array of recipients
        $messageSubject = "Disk Space Alert: Drive $($disk.Name)"
        Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $messageSubject -Body $message -SmtpServer $smtpServer

# To schedule this script to run periodically, save it as MonitorDiskSpace.ps1 and create a scheduled task in Windows Task Scheduler to run it.

How the Script Works

  1. Define the Threshold: The $threshold variable sets the minimum free space in gigabytes (GB) that you want to maintain on your drives.
  2. Get Disk Space Information: The Get-PSDrive cmdlet retrieves information about all the drives on the system.
  3. Check Each Drive: The script loops through each drive, calculates the free, used, and total space, and compares the free space against the defined threshold.
  4. Send an Alert: If the free space on any drive is below the threshold, the script composes an alert message and sends an email using the Send-MailMessage cmdlet. Multiple recipients can be specified by using an array of email addresses.

Scheduling the Script with Task Scheduler

To ensure continuous monitoring, you can schedule this PowerShell script to run at regular intervals using Windows Task Scheduler. Here’s how:

  1. Save the Script: Save the PowerShell script as MonitorDiskSpace.ps1.
  2. Open Task Scheduler: Open Windows Task Scheduler and create a new task.
  3. General Tab: Provide a name and description for the task.
  4. Triggers Tab: Create a new trigger to specify when the task should run (e.g., daily, weekly, etc.).
  5. Actions Tab: Create a new action to start a program and use the following settings:
    • Program/script: powershell.exe
    • Add arguments: -File "C:\path\to\MonitorDiskSpace.ps1"
  6. Conditions and Settings Tabs: Adjust any additional settings as needed.
  7. Save the Task: Save the task to ensure it runs as scheduled.


By using this PowerShell script and scheduling it to run regularly, you can proactively monitor disk space on your Windows systems and receive timely alerts before running into critical storage issues. This simple automation can help maintain system performance and prevent potential problems caused by insufficient disk space. Adjust the script to fit your specific requirements and enjoy peace of mind knowing your disk space is under control.

Monitoring Disk Space on Windows: A PowerShell Script for Automated Alerts

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *