Skip to content

Get disk free space with PowerShell

How to get disk capacity in GB, disk free space in GB, and disk free space in percentage with PowerShell? After we have the information, we like to copy it into a document. Do you want to have an output of the disk capacity and disk free space in percentage? In this article, you will learn how to get disk free space with PowerShell.

How to get disk free space with Disk Management

Start Disk Management and look at the columns Capacity, Free Space, and % Free.

Get disk capacity and free space percentage with PowerShell disk management

What if you want to copy all the information to a document? Unfortunately, this is not possible from Disk Management. So how can we make it possible? The solution to that is a PowerShell script.

Prepare get disk free space PowerShell script

Download Get-DiskSpace.ps1 PowerShell script and place it in C:\scripts folder. If you don’t have a scripts folder, create one.

Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.

Another option is to copy and paste the below code into Notepad. Give it the name Get-DiskSpace.ps1 and place it in the C:\scripts\ folder.

# Name the server where this needs to be run
$ServerName = 'localhost'

# Get all volumes on the server
$Volumes = Get-WmiObject -Namespace "root/cimv2" -ComputerName $ServerName -Class Win32_Volume |
Where-Object { $_.Capacity -gt 0 -and ($_.DriveType -eq 2 -or $_.DriveType -eq 3) }

# Create custom properties
$TCapacity = @{
    Expression = { "{0,19:n2}" -f ($_.Capacity / 1GB) }
    Name       = 'Total Capacity (GB)'
}

$Freespace = @{
    Expression = { "{0,15:n2}" -f ($_.FreeSpace / 1GB) }
    Name       = 'Free Space (GB)'
}

$PercentFree = @{
    Expression = { [int]($_.Freespace * 100 / $_.Capacity) }
    Name       = 'Free (%)'
}

# Display the formatted data
$Volumes | Select-Object -Property Name, $TCapacity, $Freespace, $PercentFree | Sort-Object -Property 'Free (%)' -Descending

Change line 2, $ServerName = ‘localhost’ to the machine name you want to run the script against. For example, $ServerName = ‘DC01-2019’. If you want to get the disk space and free space of the machine you downloaded the script on, you don’t have to edit it.

Run get disk free space PowerShell script

After you save the Get-DiskSpace.ps1 script in the C:\scripts folder, you can run the script.

Start PowerShell as administrator and run the script to get the free disk space.

C:\scripts\.\Get-DiskSpace.ps1

In our example, the below output appears.

Name                                              Total Capacity (GB) Free Space (GB) Free (%)
----                                              ------------------- --------------- --------
G:\                                                             19.81           18.87       95
H:\                                                              9.94            9.00       91
\\?\Volume{c482e38b-8738-4969-bcc8-57913004c6cd}\                0.44            0.14       31
F:\                                                              9.94            2.61       26
C:\                                                             59.45           11.35       19

Total Capacity (GB) and Free Space (GB) show the result with two decimals. The list is sorted on available free space in percentage.

Now that we have the information, we can copy the output and paste it into the document.

What if you want another decimal value? For example, one or three decimals as output. Change line 10 and line 15 from n2 (two decimals) to n1 (one decimal) or n3 (three decimals).

Keep reading: Check free disk space on Windows with PowerShell script »

Conclusion

You learned how to get disk capacity and disk free space percentage with PowerShell. First, download the Get-DiskSpace.ps1 script. After that, edit the name of the server or the client in the script and run the script. As of last, copy the output.

Did you enjoy this article? You may also like Export AD users to CSV with PowerShell. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 2 Comments

  1. Thank you for posting this excelling article and script. It does exactly what I was working on. However, Get-WMIObject is being deprecated for security reasons. and instead Get-CIMInstance is now used, and in fact at my job we cannot use Get-WMI on our network. But this is great Powershell practice for me converting your script to use Get-CIM instead of Get-WMI.
    Thank you,
    Barry

Leave a Reply

Your email address will not be published. Required fields are marked *