kwizcom banner advertisement

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 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:

#Purpose: Script to check the drive space in the server against the threshold value and triggers an alert
#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 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

How to Add Attachment to Email:
$attachmentPath = "C:\Reports\"

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

Tail: 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.
Drive Space Monitoring using PowerShell Script Drive Space Monitoring using PowerShell Script Reviewed by Salaudeen Rajack on 2:47 PM Rating: 5

1 comment:

  1. 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.


Please Login and comment to get your questions answered!

Powered by Blogger.