Disk Space Monitoring and Early Warning with PowerShell

Comments 49

Share to social media


Monitoring server resources is an important job of any Systems Administrator. Lose track of what resources are being allocated where, and how much of these resources are currently free, and you could find your carefully maintained, delicately balanced servers coming to a grinding halt. Well, not in all cases, but in the case that you have an OS and a database or database log file disk, you really don’t want it running out of disk space.

I recently had a bad spate of disk space issues on an Exchange 2010 SP1 server, causing Back Pressure levels to increase, which resulted in mail delivery from the Internet ceasing to function. Needless to say, the company was not thrilled, but this particular client’s budget was tight, so the server was running on minimal resources (naturally, mainly in terms of disk space). To get around that, I needed some kind of early warning system that would alert the client, myself, or both, whenever the space on certain drives was getting low. This is where PowerShell and a disk space monitoring script came in.

In this write-up, I will use Exchange 2010 (SP1) Back Pressure as an example of where you could use a script like this, besides it being used as part of a daily reporting system. The primary goal will be to write a PowerShell script which will send us a regular email report on server disk space resources. By doing this, we can aim to keep a closer eye on specific disk space figures, and therefore have a better idea of when we may be approaching any thresholds.

This PowerShell script is not going to be specifically for monitoring your Exchange servers – it is quite flexible and can be used in just about any Windows environment to monitor your servers. You’ll basically feed it a list of servers to watch over, and it will report back on these for you, meaning you could also use it as a more general “daily server disk space report” if you wish.

Disk Space Monitoring and Exchange 2010 SP1 Back Pressure

Back Pressure is when Exchange 2010 attempts to prevent “service unavailability” (when crucial resources are under pressure, such as Memory or Hard Disk space). This resource monitoring system is a service that exists on Microsoft Exchange Server 2010 Hub Transport and Edge Transport servers. Here is a list of items monitored by Back Pressure, taken from the official Technet article.

  • Free space on the hard disk that stores the message queue database.
  • Free space on the hard disk that stores the message queue database transaction logs.
  • The number of uncommitted message queue database transactions that exist in memory.
  • The memory that’s used by the EdgeTransport.exe process.
  • The memory that’s used by all other processes.

A back pressure problem is fairly easy to find in your Hub/Edge Transport server event logs once you have seen one before.

There are three levels of resource utilisation that the back pressure feature watches for. This list is also taken from a Technet article, and gives a brief explanation of what happens under each level:

  • Normal – The resource isn’t overused. The server accepts new connections and messages.
  • Medium – The resource is slightly overused. Back pressure is applied to the server in a limited manner. Mail from senders in the authoritative domain can flow. However, depending on the specific resource under pressure, the server uses tarpitting to delay server response or rejects incoming MAIL FROM commands from other sources.
  • High – The resource is severely overused. Full back pressure is applied. All message flow stops, and the server rejects all new incoming MAIL FROM commands.

Now, while it was an unwelcome encounter with Exchange Server Back Pressure that led me to pursue this script solution, I decided to focus on creating a script that would be usable in any Windows Server environment. While I’m keen to create a deeper integration with Exchange at a later date, I felt that a broader approach would be more useful for now!

Monitoring and Reporting on Disk Space using PowerShell

Previously I have only ever really delved into scripting with VBscript and batch files. I had taken a brief look at PowerShell before, but never really had the time to learn more about it. I figured this would be a nice little project to start using PowerShell on, as I do tend to learn quicker with practical projects. Given that I’m a PowerShell beginner, I’m sure there’s plenty of room for improvement, so if you do notice any areas that could be improved, please chime in with a comment at the end of this article!

So, enter PowerShell. My first impressions are that it is fairly easy to learn, and that you seem to be able to accomplish a lot with a comparatively small amount of script. Most Windows 7 installations, all Windows Server 2008 R2 installations (bar Core edition) come with PowerShell version 2.0 installed by default, and this is what the script below is written to work in. PowerShell 2.0 is included in SP2 for Windows Server 2003, so if you are running an older OS, you should be fine here too.

If you have never used PowerShell on your system before, chances are that your PowerShell “Execution Policy” is set to restrict execution of scripts on your machine, and you’ll have trouble running this script. To allow your scripts to execute, you need to set your Execution Policy to RemoteSigned. Here is the procedure to, first of all, check what yours is set to, and then, if necessary, set it to RemoteSigned.

  1. Run PowerShell as Administrator on your PC/Server
  2. Enter in and run the Get-ExecutionPolicy cmdlet – this will output the current setting. If it is not already RemoteSigned, or Unrestricted, then use the following cmdlet to set it to allow your scripts to run:

    Set-ExecutionPolicy RemoteSigned

  3. You should now be asked to confirm whether you are sure. Press Y to confirm, and then press Enter.


Figure 1 – Setting your execution policy

Now that your environment is ready to run some cmdlets and scripts, let’s take a look at some of the code we will be using. I have added comments to each of the sections, explaining what is happening at each stage of the script. The end result will email you a neat, tabled report with all the disk space information you need about any disk drives that are below a specified free disk space threshold percentage.

A basic rundown of the script’s processes goes like this:

  1. Iterate through a list of servers you specify in a text file, checking disk space.
  2. Check each free disk space percentage figure against a pre-defined percent threshold figure.
  3. If the disk in question is below this threshold, then add the details to the report, if not, skip past it.
  4. Assemble an e-mail and send it off to the specified recipient(s) if any of the drives were below the free disk space threshold.

The nice thing about this script is that it will only report on disks that need your attention (you set the threshold yourself in the script). Therefore, you will only be bothered with an email if one or more disks in your servers are really getting low on disk space.

Don’t forget to set up your from and to email address as well as your SMTP (mail) server address.

The Script

Finally, to run this script you will need a text file, created in your script folder, that it uses as an argument. Call this list.txt and place it in your script folder. In the list.txt file, list each of the servers on your domain that need to be checked in the script, one server name per line. To run the script, create a batch file called start.bat, and use the following as its content:

powershell.exe -command “& ‘C:\My Scripts\diskspace.ps1’ ‘C:\My Scripts\list.txt’ “

Naturally, remember to modify the paths in the batch file to match your environment (and note that the above batch file allows you to have blank spaces in your script’s path, too). Once this is all ready, you can use Windows Task Scheduler to schedule in the batch file to run at a certain time or interval.


Whether you are looking for a script to actively monitor your server and guard against Exchange disk space back pressure, or you are just after a daily disk space report for your servers, this should do the job. It even goes one step further by trying to be non-obtrusive, emailing you only when disks are getting low on space. It doesn’t have to end here, though. Why not improve on the above script? You could use many other PowerShell cmdlets to provide additional information or checks. Some ideas that come to mind are:

  • Find certain processes running in memory and check to see if they are using more than a certain amount or percent of RAM – if so, report on those too.
  • Find the total amount of memory in each server or computer, work out the amount of free memory, then see if this amount is below a certain percentage and report on that.
  • If working out the percentage of free disk space or memory for Exchange Back Pressure, make some additional checks and calculations based on the technet article’s explanations of low, medium and high resource pressure percentages, and report on these different scenarios (or just before each scenario is reached, to provide ample warning).

Indeed, my fellow Simple-Talk author Laerte Junior has written extensively about using PowerShell to automate your morning checklist, and is now starting to employ WMI in his scripts. Powerful stuff, making these kinds of reporting tasks elegant and versatile.

Sometimes system administrators don’t always have the time to jump on to every server console and check on their health and resources. Or you may be lucky enough to have a central monitoring console that is fed with all kinds of information about each of your servers. If it is the former, then by monitoring your server resources more closely via a script such as the one we’ve just seen, you should be able to proactively sort out any issues before they arise. This could save you precious time which would have otherwise been used to fire fight the issue at hand.