You like to remove an Address Book Policy (ABP) with PowerShell in Exchange. Let's show…
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.
Table of contents
How to get disk free space with Disk Management
Start Disk Management and look at the columns Capacity, Free Space, and % Free.
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'
# 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 2, $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 save the Get-DiskSpace.ps1 script in the C:\scripts folder, you can run the script. Start PowerShell as administrator. Change the path to the scripts folder and run the script to get the free disk space.
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 as output. Change line 7 and line 14 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.
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
You’re welcome, Barry.