Get All the windows drive report
Use the PowerShell code to get all the Windows servers' drive reports in one go.
<#
.SYNOPSIS
Collects drive space details from a list of Windows servers and emails an HTML report.
.NOTES
- Requires WinRM/CIM access to target servers (Enable-PSRemoting on targets, or WMI/DCOM open via firewall).
- Run under an account with admin rights on target servers, or supply -Credential.
- Works on Windows PowerShell 5.1 (built-in Send-MailMessage). For PS7, see notes at bottom.
#>
# ---- CONFIG ----
# V1 Enhancement 1:
# Read server names from an external text file instead of hard-coding them.
$serverListPath = "C:\Scripts\servers.txt"
$servers = Get-Content $serverListPath |
Where-Object { $_.Trim() -ne "" }
$smtpServer = "smtp.yourcompany.com"
$from = "monitoring@yourcompany.com"
$to = "you@yourcompany.com"
$subject = "Windows Servers - Drive Space Report - $(Get-Date -Format 'yyyy-MM-dd')"
$freeThreshold = 20 # percent free below which status = Check Now
# ---- COLLECT DATA ----
$results = foreach ($server in $servers) {
try {
$disks = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $server -Filter "DriveType=3" -ErrorAction Stop
foreach ($disk in $disks) {
$sizeGB = [math]::Round($disk.Size / 1GB, 2)
$freeGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$usedGB = [math]::Round($sizeGB - $freeGB, 2)
$pctFree = if ($sizeGB -gt 0) { [math]::Round(($freeGB / $sizeGB) * 100, 1) } else { 0 }
$status = if ($pctFree -lt $freeThreshold) { "Check Now" } else { "Healthy" }
[PSCustomObject]@{
Server = $server
DeviceID = $disk.DeviceID
SizeGB = $sizeGB
UsedGB = $usedGB
FreeGB = $freeGB
PercentFree = $pctFree
Status = $status
}
}
}
catch {
[PSCustomObject]@{
Server = $server
DeviceID = "-"
SizeGB = $null
UsedGB = $null
FreeGB = $null
PercentFree = $null
Status = "Server not reachable"
}
}
}
# ---- BUILD HTML TABLE MANUALLY (for per-cell color coding) ----
$style = @"
<style>
table { border-collapse: collapse; font-family: Segoe UI, Arial, sans-serif; font-size: 13px; }
th, td { border: 1px solid #ccc; padding: 6px 12px; text-align: left; }
th { background: #2c3e50; color: white; }
tr:nth-child(even) { background: #f4f4f4; }
.healthy { color: green; font-weight: bold; }
.checknow { color: red; font-weight: bold; }
.unreachable { color: red; font-weight: bold; background: #ffe5e5; }
</style>
"@
$rows = foreach ($r in $results) {
$statusClass = switch ($r.Status) {
"Healthy" { "healthy" }
"Check Now" { "checknow" }
"Server not reachable" { "unreachable" }
default { "" }
}
"<tr>
<td>$($r.Server)</td>
<td>$($r.DeviceID)</td>
<td>$($r.SizeGB)</td>
<td>$($r.UsedGB)</td>
<td>$($r.FreeGB)</td>
<td>$($r.PercentFree)</td>
<td class='$statusClass'>$($r.Status)</td>
</tr>"
}
$html = @"
$style
<h2>Windows Server Drive Space Report - $(Get-Date -Format 'yyyy-MM-dd')</h2>
<table>
<tr>
<th>Server</th>
<th>DeviceID (Drive)</th>
<th>SizeGB</th>
<th>UsedGB</th>
<th>FreeGB</th>
<th>PercentFree</th>
<th>Status</th>
</tr>
$($rows -join "`n")
</table>
<br>
<p>
Regards,<br>
<strong>SQL DBA Team</strong><br>
Database Administration
</p>
"@
# ---- SEND EMAIL ----
Send-MailMessage -From $from -To $to -Subject $subject -Body $html -BodyAsHtml -SmtpServer $smtpServer
<#
---- PS7 NOTE ----
Send-MailMessage is deprecated but still functional on Windows PowerShell 5.1 (default on Server OS).
On PowerShell 7+, replace the Send-MailMessage line with a System.Net.Mail.SmtpClient call, e.g.:
$msg = New-Object System.Net.Mail.MailMessage($from, $to, $subject, $html)
$msg.IsBodyHtml = $true
$smtp = New-Object System.Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)
For all the disks"
$disks = Get-CimInstance -ClassName Win32_LogicalDisk `
-ComputerName $server `
-Filter "DriveType=3" `
-ErrorAction Stop
ONLY C: drive
$disks = Get-CimInstance -ClassName Win32_LogicalDisk `
-ComputerName $server `
-Filter "DeviceID='C:'" `
-ErrorAction Stop
#>