How to Check Free Disk Space in PowerShell?

Check Free Disk Space in PowerShell?

PowerShell is a powerful scripting language that helps manage Windows systems efficiently—including monitoring and managing disk space. PowerShell provides reliable solutions if you need to check free disk space on your local computer, calculate the free space percentage, or even retrieve this data from a remote system.

With just a few commands, you can list all drives on your system and check the available free space, making tracking disk usage and optimizing storage easier.

Two of the most commonly used cmdlets for retrieving disk space information are:

  • Get-Volume – Provides detailed information about storage volumes, including free space.
  • Get-PSDrive – Retrieves information about all available drives, including logical and network drives.

In this article, we’ll explore how to use PowerShell to check free disk space and efficiently manage storage.

1. Using Get-Volume to Check Free Disk Space

The Get-Volume cmdlet provides a straightforward approach to retrieving disk space information. This command displays detailed information about all storage volumes on your system.

Checking All Drives

To view disk space information for all drives on your system, simply execute:

Get-Volume

This command returns a well-organized table containing essential information about each volume:

  • DriveLetter: The assigned letter for the volume
  • FileSystemLabel: The name of the volume (if assigned)
  • FileSystem: The file system type (NTFS, FAT32, ReFS, etc.)
  • DriveType: Whether the drive is fixed, removable, or network-based
  • HealthStatus: The current health condition of the volume
  • SizeRemaining: Available free space
  • Size: Total capacity of the volume

Checking a Specific Drive

To focus on a particular drive, use the -DriveLetter parameter:

Get-Volume -DriveLetter C

This command filters the output to display information only for the C: drive, making it easier to monitor your system drive specifically.

Creating a More User-friendly Output

For improved readability, you can format the output to display sizes in gigabytes with proper rounding:

Get-Volume | Select-Object DriveLetter, FileSystemLabel, 
@{Name="TotalSize(GB)";Expression={[math]::Round($_.Size/1GB, 2)}}, 
@{Name="FreeSpace(GB)";Expression={[math]::Round($_.SizeRemaining/1GB, 2)}}, 
@{Name="FreePercent";Expression={[math]::Round(($_.SizeRemaining/$_.Size)*100, 2)}}

This enhanced command adds a calculated percentage field, making it easier to assess how full each drive is at a glance.

2. Leveraging Get-PSDrive for Expanded Information

The Get-PSDrive cmdlet offers a broader perspective, as it retrieves information about all drive types recognized by PowerShell, including logical drives, network drives, and even registry hives.

Listing All Drives

To get a complete overview of all drives:

Get-PSDrive -PSProvider FileSystem

The -PSProvider FileSystem parameter filters the results to show only actual disk drives, excluding registry drives and other special PowerShell drives.

Checking a Specific Drive

For targeted information about a particular drive:

Get-PSDrive C

This command displays detailed information specifically for the C: drive.

Creating a Comprehensive Drive Report

For a more detailed report focused on storage metrics:

Get-PSDrive -PSProvider FileSystem | Select-Object Name, 
@{Name="TotalSize(GB)";Expression={[math]::Round($_.Used/1GB + $_.Free/1GB, 2)}}, 
@{Name="UsedSpace(GB)";Expression={[math]::Round($_.Used/1GB, 2)}}, 
@{Name="FreeSpace(GB)";Expression={[math]::Round($_.Free/1GB, 2)}}, 
@{Name="UsedPercent";Expression={[math]::Round(($_.Used/($_.Used + $_.Free))*100, 2)}}

This formatted output provides a comprehensive overview with both absolute values and percentages.

3. Using WMI/CIM for Advanced Disk Space Analysis

The Windows Management Instrumentation (WMI) and Common Information Model (CIM) interfaces provide powerful methods for retrieving detailed system information. These approaches offer additional details and work well in remote administration scenarios.

Using Get-WmiObject

The traditional Get-WmiObject cmdlet can retrieve detailed disk information:

Get-WmiObject -Class Win32_LogicalDisk | 
Where-Object {$_.DriveType -eq 3} |
Format-Table DeviceId, VolumeName, 
@{Name="TotalSize(GB)";Expression={[math]::Round($_.Size/1GB, 2)}}, 
@{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB, 2)}}, 
@{Name="FreePercent";Expression={[math]::Round(($_.FreeSpace/$_.Size)*100, 2)}}

The Where-Object {$_.DriveType -eq 3} filter ensures that only fixed local drives are displayed, excluding removable drives, network drives, and other special drive types.

Using Get-CimInstance (Modern Approach)

The newer Get-CimInstance cmdlet is the preferred method in modern PowerShell environments:

Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | 
Select-Object DeviceId, VolumeName,
@{Name="TotalSize(GB)";Expression={[math]::Round($_.Size/1GB, 2)}}, 
@{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB, 2)}}, 
@{Name="UsedSpace(GB)";Expression={[math]::Round(($_.Size - $_.FreeSpace)/1GB, 2)}},
@{Name="FreePercent";Expression={[math]::Round(($_.FreeSpace/$_.Size)*100, 1)}}

This approach offers better performance, particularly in remote scenarios, and follows Microsoft’s recommended practices for PowerShell scripting.

Monitoring Remote Systems

One of PowerShell’s strengths is its ability to manage remote systems. You can easily check disk space on remote computers using the -ComputerName parameter with CIM commands.

Checking a Single Remote Computer

Get-CimInstance -ComputerName "RemotePC" -ClassName Win32_LogicalDisk -Filter "DriveType=3" | 
Select-Object DeviceId, 
@{Name="TotalSize(GB)";Expression={[math]::Round($_.Size/1GB, 2)}}, 
@{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB, 2)}},
@{Name="FreePercent";Expression={[math]::Round(($_.FreeSpace/$_.Size)*100, 1)}}

Replace “RemotePC” with the actual hostname or IP address of the remote system you want to monitor.

Monitoring Multiple Remote Computers

To check disk space across multiple systems, you can use an array of computer names:

$computers = @("Server1", "Server2", "Workstation5")

foreach ($computer in $computers) {
    Write-Host "`nDisk information for: $computer" -ForegroundColor Cyan
    
    try {
        Get-CimInstance -ComputerName $computer -ClassName Win32_LogicalDisk -Filter "DriveType=3" -ErrorAction Stop | 
        Select-Object DeviceId, 
        @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB, 2)}}, 
        @{Name="Free(GB)";Expression={[math]::Round($_.FreeSpace/1GB, 2)}},
        @{Name="Free(%)";Expression={[math]::Round(($_.FreeSpace/$_.Size)*100, 1)}}
    }
    catch {
        Write-Host "Unable to connect to $computer. Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

This script includes error handling to gracefully manage connection issues with any of the remote systems.

Creating a Practical Disk Space Monitoring Script

Below is a complete script that combines several techniques to create a practical disk space monitoring solution:

# Disk Space Monitoring Script
# Purpose: Monitor disk space on local or remote computers and alert when space is below threshold

param(
    [string[]]$ComputerNames = @($env:COMPUTERNAME),
    [int]$ThresholdPercent = 15
)

$results = @()

foreach ($computer in $ComputerNames) {
    Write-Host "Checking disk space on $computer..." -ForegroundColor Cyan
    
    try {
        $disks = Get-CimInstance -ComputerName $computer -ClassName Win32_LogicalDisk -Filter "DriveType=3" -ErrorAction Stop
        
        foreach ($disk in $disks) {
            $freeSpaceGB = [math]::Round($disk.FreeSpace/1GB, 2)
            $sizeGB = [math]::Round($disk.Size/1GB, 2)
            $freePercent = [math]::Round(($disk.FreeSpace/$disk.Size)*100, 1)
            
            $diskInfo = [PSCustomObject]@{
                ComputerName = $computer
                DriveLetter = $disk.DeviceID
                VolumeLabel = $disk.VolumeName
                SizeGB = $sizeGB
                FreeSpaceGB = $freeSpaceGB
                FreePercent = $freePercent
                Status = if ($freePercent -lt $ThresholdPercent) { "LOW SPACE" } else { "OK" }
            }
            
            $results += $diskInfo
            
            # Alert for low disk space
            if ($freePercent -lt $ThresholdPercent) {
                Write-Host "WARNING: $($disk.DeviceID) on $computer has only $freePercent% free space!" -ForegroundColor Red
            }
        }
    }
    catch {
        Write-Host "Error connecting to $computer. $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Display formatted results
$results | Format-Table -AutoSize

# Export results to CSV if desired
# $results | Export-Csv -Path "DiskSpaceReport_$(Get-Date -Format 'yyyyMMdd').csv" -NoTypeInformation

This script accepts parameters for computer names and a threshold percentage, making it highly versatile for different environments.

PowerShell provides a robust toolkit for monitoring and managing disk space across Windows environments. By leveraging cmdlets like Get-Volume, Get-PSDrive, and Get-CimInstance, administrators can efficiently track storage usage, identify potential issues before they become critical, and maintain optimal system performance.

Whether managing a single workstation or an enterprise network, these PowerShell techniques can be adapted to meet your specific requirements, helping you implement an effective storage management strategy that scales with your needs.