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 to 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. 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 does not give you a prompt to save the file, right-click on Get-DiskSpace.ps1 and click save link as. You should be able to save the script.

# 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-WmiObject -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 have 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) is showing 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 in 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: Get mailbox database size and white space »

Conclusion

In this article, you learned how to get disk capacity and disk free space percentage with PowerShell. Download the Get-DiskSpace.ps1 script. Edit the name of the server in the script and run the script. Copy the output.

Did you enjoy this article? You may also like Connect to Exchange Online PowerShell using MFA. 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 0 Comments

Leave a Reply

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