In today’s technology-driven world, managing disk space efficiently is crucial to ensuring the smooth functioning of your computer systems or servers. As files and applications continue to accumulate, it’s essential to monitor and analyze disk space regularly.
Thankfully, PowerShell, a powerful scripting language and automation framework from Microsoft, provides several convenient methods to check disk space effortlessly.
In this blog post, we will explore different PowerShell commands and scripts to get free disk space on your Windows machines.
Table of Contents
PowerShell Check Disk Space: Get-Volume
The Get-Volume cmdlet in PowerShell lets you retrieve information about your computer’s volumes (logical drives).
Check Free Disk Space on All Volumes
To check the free disk space of all available volumes, open a PowerShell window and execute the following command:
Get-Volume
This command will display a detailed list of all volumes, their capacity, free space, and other essential information. The output will help you identify which volumes run low on disk space.
Check Free Disk Space on a Specific Drive Letter
By specifying the drive letter, you can limit the result to a specific volume. For example, the below command returns the free disk space information on drive D.
Get-Volume -DriveLetter D
Check Free Disk Space Based on Drive Type
In most cases, monitoring the disk space only applies to fixed disks. Using the Where-Object cmdlet, you can filter the output to show only the fixed disks.
This command gets all volumes information and filters the DriveType parameter matching the word ‘Fixed’.
Get-Volume | Where-Object {$_.DriveType -eq 'Fixed'}
Check Free Disk Space on Volumes with Assigned Drive Letters
Another way to filter results is to show only fixed drives with assigned drive letters. Take the sample command in the previous example. But this time, add another condition to show only volumes with non-empty drive letters.
Get-Volume | Where-Object {$_.DriveType -eq 'Fixed' -and $_.DriveLetter}
PowerShell Check Disk Space: Get-PSDrive
Another useful cmdlet for checking disk space is Get-PSDrive. While Get-Volume is specific to volumes, Get-PSDrive provides information about all the available drives, including file system drives and registry drives.
Check Free Disk Space on All Drives
Running this command will give you a list of drives and their properties, including available free space.
Get-PSDrive
Check Free Disk Space on FileSystem Drives Only
But as you can see, the command returned all PowerShell drives. But we’re only interested in the FileSystem provider. To get only the FileSystem drives, let’s use the -PSProvider parameter.
Get-PSDrive -PSProvider FileSystem
Check Free Disk Space on Specific Drives
You can also get free disk space for specific drives only by specifying the drive letter using the -Name parameter.
Get-PSDrive -Name C
PowerShell Check Disk Space: Win32_LogicalDisk Class
WMI (Windows Management Instrumentation) is a powerful feature in Windows that allows you to access system information, local or remote. In it, there are classes that expose this system information that you can query.
One such class is the Win32_LogicalDisk class, which includes the disk name, size, and free space. You can query this WMI class using Get-WmiObject or Get-CimInstance.
Both cmdlets produce the same result, given that the underlying data source is the same. However, Get-WmiObject is the older model, and Microsoft is moving forward to CIM-based models. But for this tutorial, you can use either command.
Check Free Disk Space on a Local or Remote Computer
For example, the following PowerShell check disk space commands query the disk information from the remote computer called DC1.
Get-CimInstance -Class Win32_LogicalDisk -ComputerName DC1 | ` Format-Table SystemName, DeviceID, DriveType, VolumeName, Size, FreeSpace -AutoSize
Note. If querying the local computer, remove the -ComputerName parameter entirely or replace the computer name with a dot (i.e., -ComputerName .)
Note that the FreeSpace and Size values are shown in bytes.
Check Free Disk Space on Multiple Computers
You can also query multiple machines at once by specifying their hostnames in the -ComputerName parameter. For example, to query DC1 and DC2, the command is:
Get-CimInstance -Class Win32_LogicalDisk -ComputerName DC1, DC2 | ` Format-Table SystemName, DeviceID, DriveType, VolumeName, Size, FreeSpace -AutoSize
Note. The Win32_LogicalDisk class does not return network-mapped drives on remote computers. To get mapped drives on remote machines, use the Win32_MappedLogicalDisk class instead.
Check Free Disk Space with WMI Filter
While a machine can have different drive types, monitoring mostly makes sense only on local disks. When running the PowerShell check disk space query with Get-WMIObject or Get-CIMInstance, you can filter the drive type to return.
Below is the list of available drive types:
- 0 = Unknown
- 1 = No Root Directory
- 2 = Removable Disk
- 3 = Local Disk
- 4 = Network Drive
- 5 = Compact Disc
- 6 = RAM Disk
We know that the fixed or local drive type number is 3. So we can add the -Filter “DriveType = 3” filter.
Get-WmiObject -Class Win32_LogicalDisk -ComputerName DC2 -Filter "DriveType = 3" | ` Format-Table SystemName, DeviceID, DriveType, VolumeName, Size, FreeSpace -AutoSize
PowerShell Script to Check Disk Space on Multiple Servers
Running the PowerShell check disk space commands manually is suitable for ad-hoc work. But it would be best to convert the commands into a reusable script or function that anyone can use without modifying the code.
Here’s a PowerShell script called Get-DiskSpace. You can also download this script from this Gist. This function can retrieve the disk space information on local and remote computers and display them in an easy-to-understand result.
Function Get-DiskSpace { [CmdletBinding()] param ( [Parameter()] [String[]] $ComputerName = $env:COMPUTERNAME ) # Drive type lookup table $driveType = @{ 0 = 'Unknown' 1 = 'No Root Directory' 2 = 'Removable Disk' 3 = 'Local Disk' 4 = 'Network Drive' 5 = 'Compact Disc' 6 = 'RAM Disk' } $result = [System.Collections.ArrayList]@() foreach ($computer in $ComputerName) { try { if ($localLogicalDisks = Get-CimInstance -Class Win32_LogicalDisk -ComputerName $computer -Property * -ErrorAction Stop) { $result.AddRange($localLogicalDisks) if (($localLogicalDisks.DriveType) -notcontains 4) { if ($mappedLogicalDisks = Get-CimInstance -Class Win32_MappedLogicalDisk -ComputerName $computer -Property * -ErrorAction Stop) { $result.AddRange($mappedLogicalDisks) } } } } catch { "[ERROR][$computer] : $($_.exception.message)" | Out-Default } } if ($result) { $result | ForEach-Object { [PSCustomObject]@{ ComputerName = $_.SystemName DeviceID = $_.DeviceID VolumeName = $_.VolumeName DriveType = $( if ($_.DriveType) { $driveType[$([int]$_.DriveType)] } else { $driveType[4] } ) SizeGB = $([System.Math]::Round(($_.Size / 1GB), 2)) UsedSpaceGB = $([System.Math]::Round((($_.Size - $_.FreeSpace) / 1GB), 2)) FreeSpaceGB = $([System.Math]::Round(($_.FreeSpace / 1GB), 2)) FreeSpacePercent = $([System.Math]::Round((($_.FreeSpace / $_.Size) * 100), 2)) } } } }
How to Use this PowerShell Check Disk Space Function?
Save the script on your computer and name it Check-DiskSpace.ps1. Open PowerShell and change the working directory to where you saved the script.
When you run the script without the -ComputerName parameter, only the local computer will be queried by default.
.\Get-DiskSpace.ps1 | Format-Table
To query one or more computers, specify the computer names in the -ComputerName parameter.
.\Get-DiskSpace.ps1 -ComputerName DC1,DC2 | Format-Table
What happens if you specify a wrong or non-existing computer name? The script will display an error and skip that computer. For example, I included a DC3 in the query, but it does not exist.
You can also output the result to a CSV, JSON, Text, and other file formats by piping to the corresponding conversion cmdlets, like, Export-Csv, ConvertTo-JSON, Out-File, etc. Take it further by exporting and formatting the report in beautiful HTML files.
Conclusion
Using the above PowerShell commands and script, you can effortlessly keep track of disk space on your Windows machines, ensuring that you never run out of space and maintain optimal system performance.
In conclusion, PowerShell is a versatile tool simplifying disk space monitoring and management. Whether you want to check individual volumes, all drives, or multiple servers, PowerShell provides various methods to make the process efficient and effective.
Keeping an eye on disk space is an essential part of system maintenance, and with PowerShell, it becomes a breeze. Happy scripting!