Drive Space Monitoring using PowerShell Script

In SharePoint servers monitoring disk space is a common administrator task. Because, SharePoint servers may run out of disk space from SharePoint, IIS Logs, SQL Server transaction logs, or even from SharePoint content (If you don’t have Quotas enabled!)

There are a lot of first-party, third-party utilities available, such as SCOM to Monitor disk space. What if you don’t have such tools in your environment? No worries, Just PowerShell can do the monitoring of disk space! PowerShell disk space monitoring can be a cost saver too!

Schedule this PowerShell script in windows task scheduler(“powershell.exe D:\scripts\DriveSpaceMonitor.ps1” . Make sure PowerShell is in your “Path” environment variable!) on hourly basis. Script will run every hour and mail only when the drive’s free space goes below the provided threshold value.

PowerShell Script for disk space monitoring:

This Script checks the drive space in the server against the threshold value and triggers an alert email:

#powershell script for disk space monitoring 
$computer = get-content env:computername; #Get the server name
$percentWarning = 50; # Store the percentage warning threshold
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";
$mailbody = "The following drive(s) have less than $percentWarning% free sapce in Server: $computer`n`n";
$mailbody+="Drive`t`tTotalspace(GB)`t`t`Freespace(GB)`n --------------------------------------------------------------- `n";
$emailsubject="$computer - Low Disk Space Alert!";

# Code to send email to the SharePoint Administrator/Group
function SendEmail([string]$msg)
	$SMTPClient = new-object System.Net.Mail.smtpClient;
	$ = ""
	$MailMessage = new-object System.Net.Mail.MailMessage;
	$MailMessage.Subject = $emailsubject;
	$MailMessage.Body = $msg;
	$MailMessage.From = "";

#The following block performs the core functionality
foreach($disk in $disks) 
	$deviceID = $disk.DeviceID; 
	[float]$size = $disk.Size; 
	[float]$freespace = $disk.FreeSpace;   
	$percentFree = [Math]::Round(($freespace / $size) * 100, 2); 
	$sizeGB = [Math]::Round($size / 1073741824, 2); 
	$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);   
	if($percentFree -lt $percentWarning) 
		$drivedata += "$deviceID`t`t $sizeGB`t`t`t$freeSpaceGB`n";

#Email to be sent only if any drive has free space less than the threshold value
if($drivedata.length -gt 0)

And see it in action! (I’ve changed the threshold to 50% for testing. It can be 10%-20% in general)

Disk space PowerShell report

monitoring disk space with powershell

If you want to monitor for multiple servers, just use this trick:

#Array to hold list of Servers
$computers = @("ServerName1","ServerName2","ServerName3","ServerName4")

#Iterate through each server
foreach ($computer in $computers)
   #Place the above code inside

Alternatively, To get all SharePoint Servers, use:

#Get all SharePoint Servers
$servers = Get-SPServer | where { $_.role -ne "Invalid"}

foreach($computer in $servers)
    write-host $Computer.Name

Tail: How to Add Attachment to Email?

$attachmentPath = "C:\Reports\"

$Attachment = new-object System.Net.Mail.Attachment($attachmentPath) 

SharePoint 2010’s Health analyzer has the rule to check “Drives are running out of free space.” which can be utilized as well. But by default SharePoint health analyzer won’t send email notifications and only display to users within Central Administration.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

4 thoughts on “Drive Space Monitoring using PowerShell Script

  • I had to comment this line out and it worked for me.
    #$computer = get-content env:computername; #Get the server name
    I actual load the variable from a txt file with the list of computers.

  • Array did not work for me.Can show how to arrange the code for array part?thanks

    • Array did not work for me, I tried and placed the above code inside but did not work. Can you show how to arrange the code for array part?

  • It’s amazing how Powershell is able to do the functions supposed to be handled by first-party and third-party utilities. I learned a lot from this post. Keep it up.


Leave a Reply

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