Drive Space Information PowerShell Script

This PowerShell script retrieves and displays space information for all local fixed drives, including total size, free space, used space, and the percentage of free space.

Click to view script…
# Get-DriveSpaceInfo.ps1
# This script retrieves space information for each local drive (DriveType=3) on the computer.
# It displays the drive letter, total size, free space, used space, and percentage of free space.

# Retrieve local disk drives (DriveType=3 indicates local fixed disks)
$drives = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3"

if (!$drives) {
    Write-Host "No local drives found."
    exit
}

foreach ($drive in $drives) {
    $driveLetter = $drive.DeviceID
    $totalSize = $drive.Size
    $freeSpace = $drive.FreeSpace

    # Calculate used space and percentage of free space (if total size is not zero)
    if ($totalSize -gt 0) {
        $usedSpace = $totalSize - $freeSpace
        $percentFree = [Math]::Round(($freeSpace / $totalSize) * 100, 2)
        $totalSizeGB = [Math]::Round($totalSize / 1GB, 2)
        $freeSpaceGB = [Math]::Round($freeSpace / 1GB, 2)
        $usedSpaceGB = [Math]::Round($usedSpace / 1GB, 2)
    }
    else {
        $usedSpace = 0
        $percentFree = 0
        $totalSizeGB = 0
        $freeSpaceGB = 0
        $usedSpaceGB = 0
    }

    Write-Host "Drive: $driveLetter" -ForegroundColor Cyan
    Write-Host "  Total Size: $totalSizeGB GB"
    Write-Host "  Free Space: $freeSpaceGB GB ($percentFree`%)"
    Write-Host "  Used Space: $usedSpaceGB GB"
    Write-Host "-------------------------------------"
}