Description

This will send an email when diskspace is below 16% on an servers specified…
Please note the following.

  1. You will need three files. The below Diskspace.ps1 file, the batch file to have it scheduled, and the list.txt file
  2. you create and schedule a Batch file with the following command -
    powershell.exe -command “& ‘C:\Scripts\DiskSpace.ps1’ ‘C:\Scripts\list.txt’”
  3. the below powershell should be called Diskspace.ps1
  4. Also create a notepad file with a list of the IP addresses of the domain servers you want monitored. This will be called list.txt.
  5. Store the files in the same location. My example has them stored in c:\scripts of our DC.

I have used this for the last few years; I found it on a website:
https://vcaleechurn.me/monitoring-disk-space-with-powershell/

Source Code

$users = "admin1@domain.ie","admin2@domain.ie" # List of users to email your report to (separate by comma)
$fromemail = "sending address@domain.ie"
$server = "ExchangeServername.local" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
# Set free disk space threshold below in percent (default at 16%)
[decimal]$thresholdspace = 16

#assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above.
$tableFragment= Get-WMIObject  -ComputerName $computers Win32_LogicalDisk `
| select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} `
| Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} `
| ConvertTo-HTML -fragment 

# assemble the HTML for our body of the email report.
$HTMLmessage = @"
<font color=""black"" face=""Arial, Verdana"" size=""3"">
<u><b>Disk Space Storage Report</b></u>
<br>This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.</b>
</b>
<br>

<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;padding: 0 1.5em;}
table{color:#FFF;background:#C00;border-collapse:collapse;width:647px;border:5px solid #900;}
thead{}
thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;}
thead tr{}
td{padding:.5em 1em;}
tfoot{}
tfoot td{padding-bottom:1.5em;}
tfoot tr{}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
$tableFragment

<u><b>Add in useful information on the script if you wish here
</b></u>
</body>
"@ 


# Set up a regex search and match to look for any <td> tags in our body. These would only be present if the script above found disks below the threshold of free space.
# We use this regex matching method to determine whether or not we should send the email and report.
$regexsubject = $HTMLmessage
$regex = [regex] '(?im)<td>'

# if there was any row at all, send the email
if ($regex.IsMatch($regexsubject)) {
				send-mailmessage -from $fromemail -to $users -subject "Disk Space Monitoring Report" -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server
}

1 Spice up

Nice script THX but for de-DE or any other then en-EN en-GB use this $tableFragment= Get-WMIObject -ComputerName $computers Win32_LogicalDisk | Where-Object{$.DriveType -eq 3} | Where-Object{ ($.freespace/$.Size)*100 -lt $thresholdspace} | Select-Object __SERVER, DriveType, VolumeName, Name, @{n=‘Size (Gb)’;e={“{0:n2}” -f ($.size/1gb)}}, @{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n=‘PercentFree’;e={“{0:n2}” -f ($.freespace/$.size*100)}} | ConvertTo-HTML -fragment

I tweaked it to fit my needs and have been a happy customer. Thanks!

Hi Colin, On the $server Our smtp server uses username and password, How do I add that?

I use this on the workstation
REM This will email when flasged under 9.8GB with the computer name and free hard drive space in MB, when placed in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

@Echo off
Set "Blank= "
Set “GB10= 10000000000”
Set “TB_10= 1099511627776”

for /f “tokens=2” %%A in (
‘wmic LogicalDisk Get DeviceID^,FreeSpace ^| find /i “C:”’
) Do Set “FreeSpace=%Blank%%%A”
Set “FreeSpace=%FreeSpace:~-15%”

Echo FreeSpace=“%FreeSpace%”
Echo 10 GB=“%GB10%”

If “%FreeSpace%” gtr “%GB10%” (
Echo yes enough free space
) else (
echo “Free space is low: %FreeSpace%” | powershell -command “Send-MailMessage -From ‘%COMPUTERNAME%@XXXXX.XXX’ -To ‘XXXXX@XXXXX.XXX’ -Subject “%COMPUTERNAME%” -Body ‘Only %FreeSpace% bytes free on C: Harddrive.’ -SmtpServer ‘smtp.XXXXX.XXX’”

)