skip to Main Content

Check free disk space on Windows with PowerShell script

We want to check free disk space on all the domain-joined Windows systems. These are the Windows Server and Windows 10 systems. An excellent way to retrieve the disk space is with PowerShell. In this article, we will bulk check free disk space with PowerShell for Windows Server and Windows 10.

Prepare check free disk space PowerShell script

Download the script Get-DiskSpaceReport.ps1 or copy and paste the below code in Notepad. Give it the name Get-DiskSpaceReport.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

Set-ExecutionPolicy Unrestricted -Force
Import-Module ActiveDirectory

# Delete reports older than 60 days
$OldReports = (Get-Date).AddDays(-60)

# Location for disk reports
Get-ChildItem "C:\Temp\DiskSpaceReport\*.*" |
Where-Object { $_.LastWriteTime -le $OldReports } |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue

# Create variable for log date
$LogDate = get-date -f yyyyMMddhhmm

# Get all systems
$Systems = Get-ADComputer -Properties * -Filter { OperatingSystem -like "*Windows Server*" } |
Where-Object { $_.Enabled -eq $true } | Select-Object Name, DNSHostName | Sort-Object Name

# Loop through each system
$DiskReport = ForEach ($System in $Systems) {
    Get-CimInstance win32_logicaldisk `
        -ComputerName $System.DNSHostName -Filter "Drivetype=3" `
        -ErrorAction SilentlyContinue
}

# Create disk report
$DiskReport | Select-Object `
@{Label = "HostName"; Expression = { $_.SystemName } },
@{Label = "DriveLetter"; Expression = { $_.DeviceID } },
@{Label = "DriveName"; Expression = { $_.VolumeName } },
@{Label = "Total Capacity (GB)"; Expression = { "{0:N1}" -f ( $_.Size / 1gb) } },
@{Label = "Free Space (GB)"; Expression = { "{0:N1}" -f ( $_.Freespace / 1gb ) } },
@{Label = 'Free Space (%)'; Expression = { "{0:P0}" -f ($_.Freespace / $_.Size) } } |

# Export report to CSV file
Export-Csv -Path "C:\Temp\DiskSpaceReport\DiskReport_$logDate.csv" -NoTypeInformation -Delimiter ";"

It will look like this.

Check free disk space on Windows with PowerShell script scripts folder

Create a second folder with the name Temp and place it in the (C:) drive. After that, create a folder DiskSpaceReport and place it in the temp folder. That’s where the report will save after you run the script.

Check free disk space on Windows with PowerShell script temp folder

Do you want to change the path of the report? Remember to change the path in the PS script on lines 8 and 36.

Before you run Get-DiskSpaceReport PowerShell script

Before you run the script, pay close attention to:

  • The systems need to be online
  • Run it on a domain-joined computer
  • Administrator rights

Free disk space Windows Server

Run the PowerShell script Get-DiskSpaceReport.ps1. It will retrieve all the Windows Servers in the domain and export the disk drives to a CSV file.

Open the CSV file in C:\Temp\DiskSpaceReport. In this example, we did open the CSV file with Microsoft Excel.

Check free disk space on Windows with PowerShell script CSV Windows Server

Free disk space specific Windows Servers

Suppose you want to export the free disk space for specific Windows Servers. Instead of running the script against all the Windows systems in Active Directory, you can create and use a specific list as input.

Create a CSV file with the name ADComputers.csv in C:\Temp. Add the header Hostname and add the Windows Servers hostnames on each line.

In our example, we like to export the free disk space on two Windows Servers.

ADComputers CSV file

Replace line 15 – line 24 with the below code.

# Import CSV file
$Servers = Import-Csv C:\Temp\ADComputers.csv

# Loop through each system
$DiskReport = ForEach ($Server in $Servers) {

    # Get system
    $System = Get-ADComputer $Server.Hostname | Where-Object { $_.Enabled -eq $true } | 
    Select-Object Name, DNSHostName | Sort-Object Name
    Get-CimInstance win32_logicaldisk `
        -ComputerName $System.DNSHostName -Filter "Drivetype=3" `
        -ErrorAction SilentlyContinue
}

Free disk space Windows 10

If you want to check all the free disk space on Windows 10 client, you must get all Windows 10 clients. Edit line 16 in the PS script to:

$Systems = Get-ADComputer -Properties * -Filter { OperatingSystem -like "*Windows 10*" } |

Run the PowerShell script and check the disk report CSV file with your favorite program. In this example, it’s the application Microsoft Excel.

Check free disk space on Windows with PowerShell script CSV Windows 10

It’s great to get all Windows disk sizes in the environment.

Note: Always use monitoring software on the systems to get an alert when the disk space is getting full. The script is not available to replace a monitoring system software!

As always, it’s important to have good monitoring software running on all your systems. It will alert you immediately when all of a sudden, the disk drives are full.

Read more: Convert thick provisioned disk to thin on VMware ESXi »

Conclusion

You learned how to check free disk space on Windows with PowerShell. Get domain-joined Windows Server or Windows 10 clients in bulk and export the disk space in a report.

Use this script when you want to check free disk space and create a report. For example, if you’re going to talk with the customer and explain why you need to buy more disks. Or what’s useful is that you can see in the disk report how much free space is allocated on the systems, and maybe you don’t need it. A great way to adjust the disk volume sizes and don’t waste any extra free space.

Did you enjoy this article? You may also like Excel CSV triple quotes when saving file. 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 4 Comments

Leave a Reply

Your email address will not be published.