Monitoring with PowerShell: Conditional Disk Space monitoring

A couple of weeks ago I was talking to someone in one of the many MSP discords, and he was struggling with disk space monitoring. His RMM system is designed in such a way that whenever he added a disk space monitor to a machine it adds an entirely new component to the monitoring list. He also could not filter out if the disk is present or not. So if the machine does not have a D:\ Drive that component would fail.

Of course this was easily solved by creating a monitor in his RMM system that said “For all drives available” but then he bumped into the next issue; he could not exclude disks such as a TempDB disks that are meant to be near capacity, or disks that are very large and thus 10% could be hundreds of GB or even a couple of TB. he also had to disable monitoring for all disks if he just didn’t want to alert on a single disk. Quite annoying of course, so I figured I’d create a quick and dirty blog to show how to monitor disk space, and build in some conditions in the monitoring component itself.

The Script

So the script has some variables to enter for your own settings, and the ones I’m giving are just an example. You can feed the “Excluded drives” from your monitoring system if it supports that. We’re also only grabbing disks that have a drive letter, change line 16 if you also want to include drives without.

####### Disk settings
$DiskSettings = [PSCustomObject]@{
    LargeDiskPercent     = 1
    LargeDiskAbsolute    = 100GB
    LargeDisksThreshold  = 10TB
    MediumDiskPercent    = 5
    MediumDiskAbsolute   = 50GB
    MediumDiskThreshold  = 3TB
    SmallDiskThreshold   = 200GB
    SmallDiskPercent     = 10
    SmallDiskAbsolute    = 2GB
    ExcludedDriveLetters = "A", "B"
####### Disk settings
try {
    $Volumes = get-volume | Where-Object { $_.DriveLetter -ne $null -and $_.DriveLetter -notin $ExcludedDriveLetters }
catch {
    write-host "Could not get volumes: $($_.Exception.Message)"
    exit 1
$DisksOutOfSpace = Foreach ($Volume in $Volumes) {
    if ($volume.size -gt $DiskSettings.LargeDisksThreshold) { $percent = $DiskSettings.LargeDiskPercent; $absolute = $DiskSettings.LargeDiskAbsolute }
    if ($volume.size -lt $DiskSettings.LargeDisksThreshold) { $percent = $DiskSettings.MediumDiskPercent; $absolute = $DiskSettings.MediumDiskAbsolute }
    if ($volume.size -lt $DiskSettings.MediumDiskThreshold) { $percent = $DiskSettings.MediumDiskPercent; $absolute = $DiskSettings.MediumDiskAbsolute }
    if ($volume.size -lt $DiskSettings.SmallDiskThreshold) { $percent = $DiskSettings.SmallDiskPercent; $absolute = $DiskSettings.SmallDiskAbsolute }

    if ($volume.SizeRemaining -lt $absolute -or ([Math]::Round(($volume.SizeRemaining / $volume.Size * 100), 0)) -lt $percent ) {
            DiskName            = $Volume.FileSystemLabel
            DriveLetter         = $volume.DriveLetter
            Size                = $volume.Size
            SizeRemaining       = $volume.SizeRemaining
            PercentageRemaining = [Math]::Round(($volume.SizeRemaining / $volume.Size * 100), 0)

if ($DisksOutOfSpace) {
    "Some disks are running low on space. Please investigate"
else {
    "Healthy - No disks running out of space"

So the benefits of approaching it this way are that he is able to have just 1 disk monitoring component in his RMM system, instead of a list of 10, he is able to make conditional changes, and the alerting even offers some more insights such as the friendly name.

As a final note, I wanted to let you all know that you can now buy me a cup of coffee on Github. Click that link if you feel like I helped you out at all once, it helps support the hosting of the blog, the CTF resources, but also Azure Functions I create and keep updated for you all. I won’t make a habit of putting this under my posts and my content will always remain free. 🙂

And that’s it! as always, Happy PowerShelling.


  1. Reddit User #456329.2 April 27, 2021 at 6:07 pm

    Great Script, I really enjoy how it dynamically sets thresholds based on the disks size. Just a suggestion, I would add the following condition to the $volume variable to make sure we are only looking at fixed disks “-and $_.DriveType -eq ‘Fixed'”

  2. Gary September 21, 2021 at 1:10 pm

    Thanks for this the script works great. I’ve edited line 16 slightly to exclude 0 size disks. I also found that some Recovery disks without labels still appeared in the results despite not having a drive letter so I excluded disks with the FileSystemLabel “Recovery”

    I’m trying to make this work in Datto RMM and am struggling a little. I edited the Volume loop to push disks that have a space issue into a new variable called DiskResults so when RMM raised an alert it only showed the disks with issues in the diagnostic results:

    $DiskResults += New-Object psobject -Property $DisksOutOfSpace

    if ($DiskResults) {
    Write-Output ”
    Write-Output “Some disks are running low on space. Please investigate”
    Write-Output ”
    Write-Output ”
    Write-Output $DiskResults
    Write-Output ”
    exit 1
    } else {
    Write-Output ”
    Write-Output “Disks are Healthy”
    Write-Output ”
    Exit 0

    When I run this directly on a server where I know there is a space issue I can see it outputs $DiskResults etc. into the PowerShell console just fine. When I have the script in a componenet monitor it just doesn’t flag the disk space issue at all. Can you see anything obvious with the code above where I may have gone wrong?

    1. Gary Eastwood September 21, 2021 at 1:12 pm

      Just to add, after posting it has removed the formatting for the Start / End Result and Start / End Diagnostic tags which are the blank Write-Output lines

Leave a Reply to Gary Cancel reply

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

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