Check drive space for multiple servers using PowerShell

Here is the script to check Disk Space of multiple servers:

The output is a html file in table format. This script also highlights if free space % is less than threshold.

$servers = Get-Content servers.txt;
$output_file = "DriveSpaceReport" + (get-date -UFormat "-%Y-%m-%d-%H_%M") + ".html";
Add-Content $output_file "<html><head><title>Drive Space Report</title><style>table { border-collapse: collapse;} table, td, th { border: 1px solid black;} </style></head><body>";
Add-Content $output_file ("Report Started at: " + (Get-Date));
Add-Content $output_file "<table border='1'><tr><th>DeviceID</th><th>Size (GB)</th><th>FreeSpace (GB)</th><th>FreeSpace %</th></tr>";
foreach($server in $servers)
{
    Add-Content $output_file ("<tr><td colspan='4'>" + $server + "</td></tr>");
    
    $drivesinfo = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.DriveType -eq 3};
    foreach($driveinfo in $drivesinfo)
    {
        if(($driveinfo.FreeSpace/$driveinfo.Size)*100 -lt 10)
        { $option = "<td bgcolor='Red'>"; }
        elseif(($driveinfo.FreeSpace/$driveinfo.Size)*100 -lt 20)
        { $option = "<td bgcolor='Yellow'>"; }
        else {$option = "<td>"};
        Add-Content $output_file ("<tr><td> " + $driveinfo.DeviceId + "</td><td>" + [Math]::Round($driveinfo.Size/1GB, 2) + "</td><td>" + [Math]::Round($driveinfo.FreeSpace/1GB, 2) + "</td>"+ $option + [Math]::Round(($driveinfo.FreeSpace/$driveinfo.Size)*100, 2) + "</td></tr>");
    }
}
Add-Content $output_file "<br/><br/>";
Add-Content $output_file ("</table><br/><br/>Report Completed at: " + (Get-Date) + "</body></html>");
Invoke-Item $output_file;

The output file is as follows:

Disk Space ReportReport Started at: 07/10/2018 23:45:49

DeviceID Size (GB) FreeSpace (GB) FreeSpace %
dc01
C: 59.66 49.14 82.36
srv01
C: 59.66 49.64 83.2
E: 2 0.15 7.63

Report Completed at: 07/10/2018 23:45:49 

This script reads a .txt file for the list of servers. Save the list of servers one server per line to servers.txt and keep it in the same folder.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on skype
Share on telegram
Share on whatsapp
Share on email
Share on print

Leave a Reply

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

Post comment