---
title: "How to Use PowerShell to Get Free Disk Space [Tutorial]"
description: "Learn how to use PowerShell to get free disk space from one or more of your storage volumes to build a disk usage report."
canonical: "https://adamtheautomator.com/powershell-get-disk-space/"
---

# How to Use PowerShell to Get Free Disk Space [Tutorial]

> Learn how to use PowerShell to get free disk space from one or more of your storage volumes to build a disk usage report.

Source: https://adamtheautomator.com/powershell-get-disk-space/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use PowerShell to Get Free Disk Space \[Tutorial\]](https://adamtheautomator.com/wp-content/uploads/2021/03/How-to-Use-PowerShell-to-Get-Free-Disk-Space-Tutorial.jpg)

# How to Use PowerShell to Get Free Disk Space \[Tutorial\]

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)10 March 20213 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Querying CIM](#h-querying-cim)
*   [Limiting CIM Output Disk Name and Free Space](#h-limiting-cim-output-disk-name-and-free-space)
*   [Converting Disk Space from Bytes to Gigabytes](#h-converting-disk-space-from-bytes-to-gigabytes)
*   [Prettying up the Disk Space Output](#h-prettying-up-the-disk-space-output)
*   [Bonus: Grouping Free Space](#h-bonus-grouping-free-space)
*   [Conclusion](#h-conclusion)

If you’re using PowerShell to get free disk space on a Windows computer, you’ve come to the right place. In this tutorial, you will learn how to use PowerShell to get free disk space and monitor disk usage.

## Prerequisites

This tutorial will assume you’re on a Windows 10 or greater computer with Windows PowerShell v5.1 installed. PowerShell 6+ will most likely work fine also. It is also assumed that you’re in an Active Directory environment. If not, be sure to learn about the `Credential` property on the [_`New-CimSession` cmdlet_](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/new-cimsession?view=powershell-7.1#example-5--create-a-cim-session-to-a-computer-using-a-pscredential-object).

## Querying CIM

Every piece of Windows system information is located in [_CIM/WMI_](https://docs.microsoft.com/en-us/windows/win32/wmisdk/wmi-start-page). The WMI datastore has information on thousands of Windows systems; disk and storage information is just one.

The CIM datastore is broken down into various [_classes_](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-cimclass). WMI stores information about disks in the Win32\_LogicalDisk class. The Win32\_LogicalDisk class contains disk information such as disk name, total size, free space, etc. To query this class with PowerShell, you must create a CIM instance with the [_`Get-CimInstance` cmdlet_](https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance).

Using `Get-CimInstance` only specifying the `ClassName` parameter is the easiest way to query the class.

```powershell
Get-CimInstance -ClassName Win32_LogicalDisk
```

`Get-CimInstance` will return a PowerShell object for each storage volume found as shown below.

![Querying storage volumes with Get-CimInstance](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T115953.353.png)

Querying storage volumes with `Get-CimInstance`

Notice that `Get-CimInstance` returns a `FreeSpace` property attached to each storage volume object returned. The `FreeSpace` property is what you need to work with.

## Limiting CIM Output Disk Name and Free Space

Assuming you don’t need all of the information returned with `Get-CimInstance`, you’ll now need to limit what to see. Suppose you’d like only to see the drive letter name (`DeviceID`) and the amount of free space (`FreeSpace`). To do that, you can trim down the output with `Select-Object`.

```powershell
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,FreeSpace
```

![Limiting output to only DeviceID and FreeSpace](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T120055.674.png)

Limiting output to only `DeviceID` and `FreeSpace`

You now have a nice little report of free disk space.

## Converting Disk Space from Bytes to Gigabytes

Although you now have a command to find free disk space, something looks off. You can probably tell that `FreeSpace` is not defined in GB; it’s actually defined in bytes. Who needs that level of granularity, anyway? Let’s convert bytes to a more readable gigabytes.

To perform the conversion, you must first divide bytes by `1GB`. For example, in this instance, you know that the `C:` disk contains `210155110400` bytes of free disk space. To convert bytes to gigabytes, you can divide that number by `1GB` like below.

```powershell
210155110400 / 1GB
```

You’ll see that the `C:` drive as a total of 195GB and some change.

![Casting decimals to integers for rounding](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T120131.395.png)

Casting decimals to integers for rounding

You’re getting closer. Chances are you were looking for a nice round number but didn’t get it with that conversion. No sweat! You need to do one last thing, which is round the gigabytes to the closest integer.

One of the easiest ways to “round” decimals to the nearest integer is casting them to an integer like below. You’ll see that the number is rounded up. This method is quicker than using [_the `Round()` method_](https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-round-to-specific-decimal-place/).

```powershell
[int](210155110400 / 1GB)
```

## Prettying up the Disk Space Output

Now that you know how to convert the free space bytes into gigabytes incorporate this technique into your script. One of the best ways to do that is with [_calculated properties_](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_calculated_properties). Calculated properties allow you to run code against property values to change them.

Calculated properties allow you to define a property value as a _[scriptblock](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks)_ which is essentially a portable unit of code.

The code you have so far only returns the disk label (`DeviceID`) and free space (`FreeSpace`) displayed in bytes like below.

```powershell
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,FreeSpace
```

Now build your own `FreeSpace` property only this time, it’s represented in gigabytes.

```powershell
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }}
```

![Calculated property to round free space](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T120250.210.png)

Calculated property to round free space

That’s much better! You now have exactly the information you were looking for.

## Bonus: Grouping Free Space

In the example above, you built a command to return free space for _each_ volume. Maybe you don’t care about each volume and would rather get a _total_ free disk space from all volumes on the computer.

Maybe you’re building a script that will process many machines at once and you want output to look like this:

```powershell
ServerName FreeSpace
---------- ---------
SRV1       195
SRV2       45
```

In that case, you need to group all of the rows returned in the previous example and sum up all of the `FreeSpace` rows using the `Measure-Object` cmdlet. This cmdlet takes an input of multiple objects, reads the property, and then performs some operation.

```powershell
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }} | Measure-Object -Property 'FreeSpace (GB)' -Sum
```

You can now see that the `Sum` property returned from `Measure-Object` is 1998, which is the total amount of free space in gigabytes this computer has.

![Finding the sum of free space](https://adamtheautomator.com/wp-content/uploads/2021/03/Untitled-2021-03-02T120425.823.png)

Finding the sum of free space

Finally, remove all of the unwanted properties and just return the total amount of free space.

```powershell
(Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property DeviceID,@{'Name' = 'FreeSpace (GB)'; Expression= { [int]($_.FreeSpace / 1GB) }} | Measure-Object -Property 'FreeSpace (GB)' -Sum).Sum
```

## Conclusion

Finding disk usage and disk free space with PowerShell only takes a single line, but you must first know how to create it! In this tutorial, you learned how to use PowerShell to get free disk space and format the output to your liking.

Improve upon this code. See if you can build a script to run this code over many servers at once!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-disk-space%2F&text=How%20to%20Use%20PowerShell%20to%20Get%20Free%20Disk%20Space%20%5BTutorial%5D)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-disk-space%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-get-disk-space%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
