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";
$drivedata=[string]::Empty;
$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;
	$SMTPClient.host = "smtp.yourcompany.org"
	$MailMessage = new-object System.Net.Mail.MailMessage;
	$MailMessage.Subject = $emailsubject;
	$MailMessage.Body = $msg;
	$MailMessage.From = "DiskSpaceMonitor@crescent.com";
	$MailMessage.To.add("salaudeen.rajack@crescent.com");
	$SMTPClient.Send($MailMessage);
}

#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)
{
	$msg=$mailbody+$drivedata;
	SendEmail($msg);
}

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?

#Attachment
$attachmentPath = "C:\Reports\MonthlyRpt.zip"

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

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. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights 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.

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

    Reply
    • 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?
      Thanks

      Reply
  • 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.

    Reply

Leave a Reply

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