⚡ PowerShell 5.1+

Get Disk Space Report

Reports free/used disk space for all local fixed drives in a color-coded table, with optional CSV export and a configurable warning threshold.

By WindowsScripting.com · 1.9 KB
Get-DiskSpaceReport.ps1
<#
.SYNOPSIS
    Reports free/used disk space for all local fixed drives.

.DESCRIPTION
    Queries WMI/CIM for every local fixed drive (DriveType 3), calculates
    used/free space and percentage used, and prints a color-coded table.
    Optionally exports the report to CSV.

.PARAMETER ExportPath
    Optional path to a .csv file. If supplied, the report is also written there.

.PARAMETER WarningThreshold
    Percentage used at which a drive is flagged as a warning (default 80).

.EXAMPLE
    .\Get-DiskSpaceReport.ps1

.EXAMPLE
    .\Get-DiskSpaceReport.ps1 -ExportPath C:\Reports\disk-space.csv -WarningThreshold 90
#>

[CmdletBinding()]
param(
    [string]$ExportPath,
    [ValidateRange(1,99)]
    [int]$WarningThreshold = 80
)

$drives = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType = 3"

$report = foreach ($drive in $drives) {
    $sizeGB   = [math]::Round($drive.Size / 1GB, 2)
    $freeGB   = [math]::Round($drive.FreeSpace / 1GB, 2)
    $usedGB   = [math]::Round($sizeGB - $freeGB, 2)
    $pctUsed  = if ($sizeGB -gt 0) { [math]::Round(($usedGB / $sizeGB) * 100, 1) } else { 0 }

    [PSCustomObject]@{
        Drive       = $drive.DeviceID
        Label       = $drive.VolumeName
        SizeGB      = $sizeGB
        UsedGB      = $usedGB
        FreeGB      = $freeGB
        PercentUsed = $pctUsed
    }
}

foreach ($row in $report) {
    $color = if ($row.PercentUsed -ge $WarningThreshold) { 'Red' }
             elseif ($row.PercentUsed -ge ($WarningThreshold - 20)) { 'Yellow' }
             else { 'Green' }

    Write-Host ("{0,-4} {1,-16} {2,8:N2} GB total  {3,8:N2} GB free  {4,6}% used" -f `
        $row.Drive, $row.Label, $row.SizeGB, $row.FreeGB, $row.PercentUsed) -ForegroundColor $color
}

if ($ExportPath) {
    $report | Export-Csv -Path $ExportPath -NoTypeInformation -Encoding UTF8
    Write-Host "`nReport exported to $ExportPath" -ForegroundColor Cyan
}