skip to Main 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? You are on the right page. Let’s get into it and get the disk free space with PowerShell.

How to get disk free space with Disk Management

Start Disk Management. Have a 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

Copy the below code and paste it into Notepad. Save the file type as Get-DiskSpace.ps1 in the C:\scripts\ folder. Create a scripts folder if you don’t have one on the C: drive. You can also download the Get-DiskSpace.ps1 script (direct link). If it doesn’t prompt you to save the file, right-click on Get-DiskSpace.ps1 and click save link as.

# Created by Mohammed A. Wasay
# Edited by ALI TAJRAN / ALITAJRAN.com
 
# Name the server where this needs to be run
$ServerName = 'localhost'

# Check Total Capacity of the Drive
$TCapacity =
@{
    Expression = { "{0,19:n2}" -f ($_.Capacity / 1GB) };
    Name       = 'Total Capacity (GB)';
}
 
# Freespace to be displayed in GB
$Freespace =
@{
    Expression = { "{0,15:n2}" -f ($_.FreeSpace / 1GB) };
    Name       = 'Free Space (GB)';
}
 
# Percentage value of the free space
$PercentFree =
@{
    Expression = { [int]($_.Freespace * 100 / $_.Capacity) };
    Name       = 'Free (%)'
}
 
# Calculation
Get-CimInstance -Namespace "root/cimv2" -ComputerName $ServerName -Query "SELECT Name, Capacity, FreeSpace FROM Win32_Volume WHERE Capacity > 0 and (DriveType = 2 OR DriveType = 3)" |
 
# Display of values
Select-Object -Property Name, $TCapacity, $Freespace, $PercentFree  | Sort-Object 'Free (%)' -Descending

Change line 5, $ServerName = ‘localhost’ to the machine’s name that you want to run the script against. For example, $ServerName = ‘DC01-2016’. 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 saved the Get-DiskSpace.ps1 script in the C:\scripts folder, you can run the script. Start PowerShell as administrator. Change the directory to the scripts folder and run the script.

PS C:\> cd \scripts
PS C:\scripts> .\Get-DiskSpace.ps1

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

The results are great. Total Capacity (GB) and Free Space (GB) shows 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 showing as output. Change line 10 and line 17, n2 (two decimals) to n1 (one decimal) or n3 (three decimals).

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

Conclusion

In this article, 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 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

    1. You’re welcome, Barry.

      I changed the script to the Get-CimInstance cmdlet, so the readers will have an up-to-date script to fetch the information.

      Thanks for letting me know!

Leave a Reply

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