0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check Disk Space Usage

Use Get-PSDrive -PSProvider FileSystem | Select-Object Name, Used, Free, @{Name='Total';Expression={$_.Used + $_.Free}} to check disk space on all drives in PowerShell.
📋

Examples

InputRun script on system with C: drive having 100GB free and 50GB used
OutputName Used Free Total ---- ---- ---- ----- C 50GB 100GB 150GB
InputRun script on system with D: drive having 0GB free and 200GB used
OutputName Used Free Total ---- ---- ---- ----- D 200GB 0GB 200GB
InputRun script on system with multiple drives C: and E:
OutputName Used Free Total ---- ---- ---- ----- C 30GB 70GB 100GB E 10GB 90GB 100GB
🧠

How to Think About It

To check disk space, you ask the computer to list all drives that store files. Then, for each drive, you find how much space is used and how much is free. Adding these gives the total size. This helps you see disk usage at a glance.
📐

Algorithm

1
Get all drives that use the file system.
2
For each drive, get the used space and free space.
3
Calculate total space by adding used and free space.
4
Display the drive name along with used, free, and total space.
💻

Code

powershell
Get-PSDrive -PSProvider FileSystem | Select-Object Name, @{Name='Used(GB)';Expression={[math]::Round(($_.Used/1GB),2)}}, @{Name='Free(GB)';Expression={[math]::Round(($_.Free/1GB),2)}}, @{Name='Total(GB)';Expression={[math]::Round((($_.Used + $_.Free)/1GB),2)}} | Format-Table -AutoSize
Output
Name Used(GB) Free(GB) Total(GB) ---- --------- -------- -------- C 50.00 100.00 150.00 D 200.00 0.00 200.00
🔍

Dry Run

Let's trace checking disk space on a system with C: drive having 50GB used and 100GB free.

1

Get all file system drives

Found drive C: with Used=50GB, Free=100GB

2

Calculate total space

Total = Used + Free = 50GB + 100GB = 150GB

3

Display results

Output: Name=C, Used=50GB, Free=100GB, Total=150GB

DriveUsed(GB)Free(GB)Total(GB)
C50100150
💡

Why This Works

Step 1: Get-PSDrive command

The Get-PSDrive -PSProvider FileSystem command lists all drives that store files, like C: or D:.

Step 2: Calculate sizes in GB

We convert bytes to gigabytes by dividing by 1GB and round to 2 decimals for easy reading.

Step 3: Select and display properties

We pick the drive name, used space, free space, and total space, then show them in a neat table.

🔄

Alternative Approaches

Using WMI to get disk space
powershell
Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, @{Name='FreeSpace(GB)';Expression={[math]::Round($_.FreeSpace/1GB,2)}}, @{Name='Size(GB)';Expression={[math]::Round($_.Size/1GB,2)}} | Format-Table -AutoSize
This method uses WMI and shows free and total size but does not directly show used space.
Using CIM cmdlets for modern systems
powershell
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, @{Name='FreeSpace(GB)';Expression={[math]::Round($_.FreeSpace/1GB,2)}}, @{Name='Size(GB)';Expression={[math]::Round($_.Size/1GB,2)}} | Format-Table -AutoSize
CIM is newer and faster than WMI but similar output; used space must be calculated manually.

Complexity: O(n) time, O(n) space

Time Complexity

The script processes each drive once, so time grows linearly with the number of drives.

Space Complexity

It stores information for each drive, so space also grows linearly with the number of drives.

Which Approach is Fastest?

Using Get-PSDrive is fast and simple for local drives; WMI and CIM methods are slightly slower but provide more detailed info.

ApproachTimeSpaceBest For
Get-PSDriveO(n)O(n)Quick local disk space check
WMI (Get-WmiObject)O(n)O(n)Detailed info, legacy systems
CIM (Get-CimInstance)O(n)O(n)Modern systems, faster than WMI
💡
Use Format-Table -AutoSize to make the output easy to read in PowerShell.
⚠️
Beginners often forget to convert bytes to gigabytes, making the output hard to understand.