Home » PowerShell » How to Get Free Disk Space in PowerShell?

How to Get Free Disk Space in PowerShell?

PowerShell is a powerful scripting language that can be used to manage Windows systems, including managing disk space. It can be used to get free disk space on your local computer free disk space percentage or free disk space on the remote computer.

This information is used to get a list of all the drives on your computer and the amount of free space on each drive and manage disk space usage.

PowerShell provides several ways to get free disk space on a system. The Get-Volume and Get-PSDrive cmdlets are two of the most common ways to get disk space and usage.

In this article, we will discuss how to get free disk space using PowerShell.

How to Get Free Disk Space For All Drives

To get free disk space on a system using PowerShell, you can use Get-Volume cmdlet. This command gets a list of all the volumes available on the system, the Health Status of each drive, File System Type, total disk space allocated to the drive, and most importantly free disk space in GB.

Get-Volume

In the above example, the PowerShell Get-Volume command shows the list of all drives and their disk space information.

The Get-Volume Example output is given below.

PowerShell Get-Volume (Get free disk space)
PowerShell Get-Volume (Get free disk space)

Cool Tip: How to get disk information in PowerShell!

How to Get the Volume With Free Disk Space for a Specific Drive

Get-Volume -DriveLetter C

The Get-Volume command uses the -DriveLetter parameter to specify the drive letter. This command returns the volume object having information about the C drive, File System Type, Size Allocated in GB, and Free disk space in GB.

The Get-Volume to get free disk space for C drive Output is below.

Get-Volume - Free disk space for drive in gb
Get-Volume – Free disk space for drive in GB

The output of the Get-Volume cmdlet includes the following properties:

  • DriveLetter: The name of the drive
  • FileSystemType: It specifies the file system type.
  • DriveType: The type of drive, such as Fixed, Removable, or CDROM.
  • HealthStatus: The status of the drive.
  • OperationalStatus: The operational status of the drive.
  • SizeRemaining: The amount of free space on the drive in GB.
  • Size: The total space on the drive is in GB.

Cool Tip: How to get the current directory full path in PowerShell!

How to Get Free Available Disk Space Using Get-PSDrive Command in PowerShell

The Get-PSDrive PowerShell command returns the list of all drives, Environment, Registry name HKCU, HKLM, and total space used in GB and free available disk space in GB.

Get-PSDrive
#To get specific drive volume and size information, run below command
Get-PSDrive C

In the above example, the PowerShell Get-PSDrive command returns C drive volume object details and total size remaining in GB.

PowerShell Get-PSDrive free space in GB output as below

Get-PSDrive - Free space in gb
Get-PSDrive – Free space in gb

The output of the Get-PSDrive cmdlet includes the following properties.

  • Name: The name of the drive.
  • Used: The amount of used space on the drive in GB.
  • Free: The amount of free space on the drive in GB.
  • Provider: The name of the drive provider.
  • Root: The root directory of the drive.

Cool Tip: How to use the QUser command to get a list of users logged on to the server!

How to Check Disk Space Using win32_logicaldisk

To check the disk space, use the Get-WmiObject command in PowerShell with the class win32_logicaldisk. This command returns deviceId, total size allocated, and free space for each drive.

Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

This command in PowerShell checks disk space and gets free disk space in GB as below.

win32_logicaldisk - get disk space
win32_logicaldisk – get disk space

Cool Tip: The best way to download a zip file using PowerShell!

How to Get Disk Usage with the Get-CimInstance Command in PowerShell

Use the Get-CimInstance cmdlet in PowerShell to get free disk space on a local computer.

Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

In the above example, the Get-CimInstance command uses the -ComputerName parameter to specify the computer name, in this case, “localhost” as the local computer. This command gets free disk space information for the C drive as below.

Get-CimInstance - Get drive size
Get-CimInstance – Get drive size

You can use below PowerShell script below to get a remote computer with free disk space

Get-CimInstance -ComputerName tom-laptop win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

In the above example, the Get-CimInstance command in PowerShell takes the remote computer name as tom-laptop and gets the C drive total size allocated and free space in GB.

Cool Tip: How to create shortcuts on a user’s desktop using PowerShell!

Conclusion

I hope the above article will help you to get free disks on local or remote computers using different ways available in PowerShell.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.