Skip to 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 clients. An excellent way to retrieve the disk space is with PowerShell. In this article, we will check all free disk space with PowerShell for Windows Server, Windows 10, and Windows 11.

Prepare check free disk space PowerShell script

Download the script Get-DiskSpaceReport.ps1 or copy and paste the below code into 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.

<#
    .SYNOPSIS
    Get-DiskSpaceReport.ps1

    .DESCRIPTION
    Export all enabled Windows Servers disk space to CSV file.

    .LINK
    www.alitajran.com/check-free-disk-space-windows-powershell-script

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 02/17/2022 - Initial version
    V1.10, 11/03/2023 - Added OS name and OS version
#>

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 -Format yyyyMMddhhmm

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

# Loop through each system
$DiskReport = ForEach ($System in $Systems) {
    $OperatingSystem = $System.OperatingSystem
    $OperatingSystemVersion = $System.OperatingSystemVersion
    Get-WmiObject Win32_LogicalDisk `
        -ComputerName $System.DNSHostName -Filter "DriveType=3" `
        -ErrorAction SilentlyContinue |
    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) } },
    @{Label = "Operating System"; Expression = { $OperatingSystem } },
    @{Label = "Operating System Version"; Expression = { $OperatingSystemVersion } }
}

# Create disk report
$DiskReport |
Export-Csv -Path "C:\Temp\DiskSpaceReport\DiskReport_$LogDate.csv" -NoTypeInformation #-Delimiter ";"

It will look like this in File Explorer.

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 PowerShell script on lines 28 and 59.

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

Get free disk space for all Windows Servers

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.

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

Browse to C:\Temp\DiskSpaceReport and open the exported CSV file.

In this example, we did open the CSV file with Microsoft Excel.

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

Get free disk space for 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

Run the script below to export the free disk space report for the specific Windows Servers to a CSV file.

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 -Format yyyyMMddhhmm

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

# Loop through each server
$DiskReport = ForEach ($System in $Systems) {
    $ComputerName = $System.Hostname
    $OperatingSystem = (Get-ADComputer -Identity $ComputerName -Properties OperatingSystem).OperatingSystem
    $OperatingSystemVersion = (Get-ADComputer -Identity $ComputerName -Properties OperatingSystemVersion).OperatingSystemVersion
    Get-WmiObject Win32_LogicalDisk `
        -ComputerName $ComputerName -Filter "DriveType=3" `
        -ErrorAction SilentlyContinue |
    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) } },
    @{Label = "Operating System"; Expression = { $OperatingSystem } },
    @{Label = "Operating System Version"; Expression = { $OperatingSystemVersion } }
}

# Create disk report
$DiskReport |
Export-Csv -Path "C:\Temp\DiskSpaceReport\DiskReport_$LogDate.csv" -NoTypeInformation #-Delimiter ";"

Get free disk space for Windows clients

Get free disk disk space for Windows 10 clients.

If you want to check all the free disk space on Windows 10 clients, edit line 36 in the original PowerShell script to:

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

Get free disk space for Windows 11 clients.

If you want to check all the free disk space on Windows 10 clients, edit line 36 in the original PowerShell script to:

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

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 monitoring system software!

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, Windows 10, or Windows 11 clients in bulk and export the disk space in a report.

Use this script 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 helpful 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. This is 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 18 Comments

  1. Thank you – the script really works well!
    However, when I schedule it under the Windows Task Scheduler, running under the SYSTEM account, then it only returns the local server’s disks. I assume it is getting access denied from the other servers, due to the SYSTEM account. I also tried with the NETWORK SERVICE account, but same results.
    Any advice?

  2. Great Script, is there a way to display Hostname’s Operating System via
    @{Label = “OSVersion”; Expression = { $_.OSVersion } },

  3. Thanks for the script, works perfectly. (I did take AM’s suggestion and change the delimiter)

  4. Thanks great script, but can you please help me to get only details for C drive and the report should get by email, it would be your great help.

    Thanks in advance

    Regards
    Vikas

  5. Hi! Great script, thanks for your work and for sharing it!
    Regarding the Windows Vulnerability CVE-2021-26414 Microsoft is about to enforce an “hardened” logon for WMI Sessions. Will that have some impact on the way the script works?
    Best regards!

  6. Hi,
    I did everything shown here but I got this message on PowerShell window.

    Import-Module : The specified module ‘ActiveDirectory’ was not loaded because no valid module file was found in any
    module directory.

  7. I adjusted slightly and it does work – leveraged Get-WmiObject – since I was running against Win10 computers vs servers.

  8. Hi Ali,

    This is working fine, thanks for the amazing script. But the output in the csv file is creating everything in a single column. How to export everything in a separate column.
    HostName DriveLetter DriveName Total Capacity (GB) Free Space (GB) Free Space (%)

    Thanks in Advance

    1. Use a “;” in the delimiter (last line)

      Export-Csv -Path "C:\Temp\DiskSpaceReport\DiskReport_$logDate.csv" -NoTypeInformation -Delimiter ";"
  9. Hello,

    Thank you for your amazing script!
    But can you show the free space in gigabytes instead of percentage?
    I’m horrible at PowerShell (or scripting even), so I have no idea how to do this myself.

    Thank you!

Leave a Reply

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