Monitor Disk Space using Powershell

All administrators face this issue, a disk is running low on disk space but you don’t log on to the server that much so you don’t notify it.

That’s why I created this Powershell Script, just insert the name of you’re server in the .txt file and schedule the Powershell script as much as you would like it to run.

There are 2 options :
– Threshold mail (This wil only send an email when the specified Threshold is met
– Full mail (Every time the script runs it will mail a list of you’re servers and the disk capacity)

Function name : Get-FreeSpace

Parameters :
minimumThreshold : The value in % on which a disk will marked as almost full
HIGHPRIO :  The value in % when a send mail will be marked as High Priority
ServerList : Path to the server list txt file
Logging : Boolean value to log to screen
Maillog : Boolean value to log to mail
Mailto : Mandatory when Maillog is used (Recipient of report)
From : Mandatory when Maillog is used (Send of report)
Subject : Mandatory when Maillog is used (Mail subject)
Smtpserver : Mandatory when Maillog is used
CompleteLog : A full report of all server with diskspace will be generated and included
Bold = Mandatory

The script

Function Get-FreeSpace {
[Parameter(Mandatory=$true, HelpMessage=”Threshold Percentage”)][int] $minimumThreshold,
[Parameter(Mandatory=$true, HelpMessage=”Mark Mail as High Priority Percentage”)][int] $HIGHPRIO,
[Parameter(Mandatory=$true, HelpMessage=”Text File with Server List”)][string] $ServerList,
[Parameter(Mandatory=$false, HelpMessage=”Log to screen?”)][boolean] $Logging,
[Parameter(Mandatory=$false, HelpMessage=”Mail Log?”)][boolean] $MailLog,
[Parameter(Mandatory=$false, HelpMessage=”Mail recipient”)][string] $Mailto,
[Parameter(Mandatory=$false, HelpMessage=”Mail sender”)][string] $From,
[Parameter(Mandatory=$false, HelpMessage=”Mail Subject”)][string] $Subject,
[Parameter(Mandatory=$false, HelpMessage=”SMTP Server”)][string] $Smtpserver,
[Parameter(Mandatory=$false, HelpMessage=”List all CSV’s”)][boolean] $CompleteLog

##### [Parameter(Mandatory=$true, HelpMessage=”Server to monitor”)][array] $Servers, ##########
$datetime = Get-Date -Format “yyyyMMddHHmmss”;

# Get server list
$servers = Get-Content $ServerList
$HighPrioFlag = “normal”
$SendMail = $false

if ($MailLog)
  if ($Mailto -eq “”) { $quit = $true }
  if ($From -eq “”) { $quit = $true }
  if ($Subject -eq “”) { $quit = $true }
  if ($Smtpserver -eq “”) { $quit = $true }

  if ($quit)
    write-host -f red “When using MailLog ‘Mailto, From, Subject and Smtpserver’ are required”
# Add headers to log file
#Add-Content “$Env:USERPROFILE\server disks $datetime.txt” “server,deviceID,size,freespace,percentFree”;

$objs = @()
$FullTable = “<table><tr><td><b>Server</td><td><b>DeviceID</td><td><b>SizeGB</td><td><b>FreespaceGB</td><td><b>PercentFree</td></tr>”
$ThresholdTable = “<table><tr><td><b>Server</td><td><b>DeviceID</td><td><b>SizeGB</td><td><b>FreespaceGB</td><td><b>PercentFree</td></tr>”
foreach($server in $servers)
 # Get fixed drive info
 $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter “DriveType = 3”;
 foreach($disk in $disks)
  $obj = New-Object PSObject
  $deviceID = $disk.DeviceID;
  [float]$size = $disk.Size;
  [float]$freespace = $disk.FreeSpace;

  $obj | add-member -membertype noteproperty -name Server -value $server -force
  $obj | add-member -membertype noteproperty -name Size -value $size -force
  $obj | add-member -membertype noteproperty -name DeviceID -value $DeviceID -force

  $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
  $obj | add-member -membertype noteproperty -name PercentFree -value $percentFree -force

  $sizeGB = [Math]::Round($size / 1073741824, 2);
  $obj | add-member -membertype noteproperty -name sizeGB -value $sizeGB -force

  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
  $obj | add-member -membertype noteproperty -name FreespaceGB -value $freeSpaceGB -force

  if ($obj.PercentFree -le $HIGHPRIO)
   $HighPrioFlag = “high”
  if ($PercentFree -le $minimumThreshold)
   $SendMail = $true
   $color = “#FF0000”
   $ThresholdTable += “<tr><td>” + $obj.Server + “</td><td>” + $obj.DeviceID + “</td><td>” + $obj.SizeGB + “</td><td>” + $obj.FreespaceGB + “</td><td><b><font color=’$color’>” + $obj.PercentFree + “</font></b></td></tr>”
   $color = “#00FF00”
  $FullTable += “<tr><td>” + $obj.Server + “</td><td>” + $obj.DeviceID + “</td><td>” + $obj.SizeGB + “</td><td>” + $obj.FreespaceGB + “</td><td><b><font color=’$color’>” + $obj.PercentFree + “</font></b></td></tr>”



$FullTable += “</table>”
$ThresholdTable += “</table>”
$objs = $objs | ft -auto Server,DeviceID,SizeGB,FreespaceGB,PercentFree

if ($Logging)

if ($SendMail -OR $CompleteLog) {
if ($MailLog)
 $body = “<b>Threshold Report:</b><br>” +$ThresholdTable
 if ($CompleteLog)
 $body += “<p><b>Full Report:</b></p>” + $FullTable
 #send-mailmessage -BodyAsHtml -body $FullTable -subject $Subject -to $mailto -from $From -smtpserver $Smtpserver -Priority $HighPrioFlag

 $subject = $subject + ” less than ” + $minimumThreshold + “%”
 send-mailmessage -BodyAsHtml -body $body -subject $Subject -to $mailto -from $From -smtpserver $Smtpserver -Priority $HighPrioFlag

} #End Function Get-FreeSpace

 Download the script :

Save the script with a name (.ps1) you like on a location you like for example “d:\freespacefunction.ps1”, to use the function use :

Start you’re Powershell window
import-module “d:\freespacefunction.ps1”
Get-Freespace and all the parameters

Example of ServerList

–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Logging $true
This will output a list to screen

–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Maillog $true -mailto -from -subject “Free disk space report” -smtserver
This will send the report of only disks less than 8% freespace to, if no disks are less than 8% no mail will be send

–  Get-Freespace  -minumumThreshold 8 -Highprio 3 -Serverlist “c:\servers.txt” -Maillog $true -mailto -from -subject “Free disk space report” -smtserver -CompleteLog $true 
This will send the report of all disks to every time the script is scheduled

How to schedule a Powershell script:
See my Post :
How to schedule a Powershell script using Scheduled tasks in Windows Server 2008 R2


11 comments on “Monitor Disk Space using Powershell”

  1. Mike says:

    Seems my powershell does not like the table methods used. On import-module I get:

    The ‘<' operator is reserved for future use.
    At C:\scripts\getspace.ps1:88 char:19
    + $FullTable += "< <<<” + $obj.Server + “” + $obj.DeviceID + “” + $obj.SizeGB + ”
    ” + $obj.FreespaceGB + “” + $obj.PercentFree + “
    + CategoryInfo : ParserError: (<:OperatorToken) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : RedirectionNotSupported

    1. Michael says:

      Hi Mike,

      I think it fails on the quotes because of the font, please download the script through this link and it will work.

      Good luck

  2. Rob says:

    I’ve read your section on scheduling ps scripts with task scheduler but would you do it specifically witth this script?

    1. Michael says:

      Rob, I don’t understand you’re question, why wouldn’t you schedule it?

  3. Balu K says:

    Hi Michael,

    Excellent Work. This script is working like a charm. Many thanks.!

    I would like to know, can we do the same thing for CPU & Memory usage. If yes, how? Please help..!

    Balu K

    1. Michael says:

      Hi Balu,

      The script uses WMI, so yes it is possible to get free memory by using “Select FreePhysicalMemory, FreeVirtualMemory from Win32_OperatingSystem” for example, with “SELECT * FROM Win32_PerfRawData_PerfOS_Processor” you get all kinds of processor information. So just make some changes to my script for this information.



      1. Balu K says:

        Hi Michael,

        Thanks for the response..!
        I have tried to edit your script according to your suggestion. But unable to update as I am totally unaware on scripting. 🙁

        Sorry for being illiterate

        Please Help.

        Balu K

  4. Niju Alex says:

    Michel, Excellent script.. worked great… life saver 🙂

    As a Sys Admin I need to go through the check list every day. Now, I removed hard disk monitoring from my check list as I gettign daily report. I have scheduled your script as two tasks like DAILY REPORT & DISK ALERTS.

    It would be great, if you updated the script by including RAM, CPU & Event viewer errors in the same way.

    Eagerly waiting for your update script.

    – Niju

    1. Michael says:


      I realy don’t have the time to rewrite the script for this usage, if you would like to ease you’re work load by using scripts I must suggest diving into scripting, this can help you alot and google is filled with information about it, if you just start reading from above the script is quite ease to recreate for usage of RAM and CPU.

  5. Knic says:

    Thanks this script is outstanding. Is there an easy way to calculate a total for drive Size and Freespace in the full report?

    1. Michael says:

      Hi Knic,

      I’m sure what you mean with

      “a total for Drive Size and Freespace”

      All the sizes are totals I guess, but as you can see in the script everything is stored in a variable so you can create a new variable and add every value in this.

      You can change the script anyway you like it.



Leave a Reply

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


Please enter the CAPTCHA text

This site uses Akismet to reduce spam. Learn how your comment data is processed.