Windows OS Hub
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows 10
    • Windows Server 2025
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
    • Proxmox
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / PowerShell: Check Free Disk Space and Disk Usage

March 17, 2024

PowerShell: Check Free Disk Space and Disk Usage

In this article, we will show you how to check the free disk space and disk usage on a local or remote Windows host using PowerShell. Also, consider how to notify the administrator with a pop-up notification or email if the free space threshold is exceeded.

Contents:
  • How to Check Drive Free Space on Windows with WMI and PowerShell?
  • Get Free Disk Space from Remote Windows Hosts via PowerShell

How to Check Drive Free Space on Windows with WMI and PowerShell?

You can get information about your logical drives in Windows using the Win32_logicalDisk WMI class.

The command below will display all information about the logical drives on your computer:

Get-WmiObject -Class Win32_LogicalDisk

If you are using the newer PowerShell Core 7.x, note that WMI is not supported in this PowerShell version (since PowerShell Core is based on .Net Core). If you try to run the Get-WmiObject command, you will see the following error: The term 'Get-WmiObject' is not recognized as a name of a cmdlet, function, script file, or executable program. Use CIM instead of WMI, for example:

Get-CimInstance win32_logicaldisk

Get-CimInstance win32_logicaldisk on powershell core instead WMI

The FreeSpace property contains the amount of free space in bytes left on each of the drives. To make it more convenient, you can convert it to GB and display the amount of free space on each logical disk in % (as the ratio of free space to the total disk size). You can use the following PowerShell script:

Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID, VolumeName, @{Label='FreeSpace (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{label='FreePercent'; expression={[Math]::Round(($_.freespace / $_.size) * 100, 2)}}|ft

Get disk free space with PowerShell

The script displays a list of logical drives, their size, and free space percentage.

To use this script in PowerShell Core, just replace Get-WmiObject with Get-CimInstance.

If you don’t want to simply display the information about the free space on a disk, but take some action instead (send an e-mail or show a popup message) if there is less free space than the specified threshold, you can use the PowerShell script below:

$percentWarning = 20
$percentCritcal = 5
$ListDisk = Get-WmiObject -Class Win32_LogicalDisk
Foreach($Disk in $ListDisk){
if ($Disk.size -ne $NULL) {
$DiskFreeSpace = ($Disk.freespace/1GB).ToString('F2')
$DiskFreeSpacePercent = [Math]::Round(($Disk.freespace/$Disk.size) * 100, 2)
if($DiskFreeSpacePercent -lt $percentWarning){
$Message= "Warning!"
if($DiskFreeSpacePercent -lt $percentCritcal){
$Message= "Alert!"
}
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Disk $($Disk.DeviceID) has only $DiskFreeSpace GB of free space left",0,$Message,48)
}
}
}

How to send an alert when Disk Space is running low on Windows Servers with PowerShell (via e-mail or pop-up notification)

This script sets threshold values of free space left on a disk — 5% and 20%. If the amount of free space on any of the disks is below the specified values, a modal information window is displayed. You can show it as a pop-up notification or immediately run the Disk Cleanup tool (cleanmgr.exe).

If you would like to email the administrator of the problem, you can send an email via an SMTP server (it may be Exchange host or any other SMTP service, even the built-in Windows Server SMTP role will do) with the Send-MailMessage cmdlet:

Send-MailMessage -To “[email protected]” -From “$env:[email protected]” -Subject “Insufficient disk space on server $env:computername” -Body “Disk $($Disk.DeviceID) has only $DiskFreeSpace GB left” -Credential (Get-Credential) -SmtpServer smtp.woshub.com -Port 587

In this example, you need to enter credentials to connect to the SMTP host interactively. You can configure your SMTP host to accept messages from trusted hosts without authentication, or configure SMTP authentication using the saved credentials in the file (it is not possible to use Managed Service Accounts to send emails).

You can run the PowerShell script regularly using the Task Scheduler or it can be configured as a Windows service. If there is not enough free space on this Windows host, an administrator will get a notification.

Get Free Disk Space from Remote Windows Hosts via PowerShell

The Invoke-Command cmdlet can be used to run a PS script to check the remaining free space on remote computers.

Invoke-Command -ComputerName srv01,srv02,srv03 -FilePath "C:\PS\checkfreespace.ps1"

If the servers you want to check the amount of free space on are in your domain, you can get the list of them from Active Directory using the Get-ADComputer cmdlet and run the script against each host:

$computers = (Get-ADComputer -Filter 'operatingsystem -like "*Windows Server*" -and enabled -eq "true"').Name
Invoke-Command -ComputerName $computers -FilePath "C:\PS\checkfreespace.ps1" -ErrorAction SilentlyContinue

You can also use RemoteWMI to get WMI data from remote computers:

Get-WmiObject -Class Win32_logicalDisk -ComputerName srv01,srv02,srv03

This guide describes the easiest self-made solution to monitor disk space. If you have a lot of hosts that need to be monitored, it is better to use a full-featured monitoring system (like Zabbix, Icinga, PRTG, Nagios, etc.).
2 comments
9
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows Server 2016
previous post
Sending Email with SMTP Authentication via Telnet or OpenSSL
next post
How to Check Who Created a User Account in AD

Related Reading

PowerShell: Get Folder Size on Windows

April 2, 2024

How to Download Offline Installer (APPX/MSIX) for Microsoft...

March 12, 2024

How to Refresh (Update) Group Policy Settings on...

August 13, 2024

Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)

March 17, 2024

How to Backup and Restore Websites and IIS...

June 8, 2023

Slow Access to Shared Folders and Network Drives...

March 11, 2024

How to Uninstall Built-in UWP (APPX) Apps on...

June 6, 2024

Repairing the Domain Trust Relationship Between Workstation and...

May 16, 2024

2 comments

Leslie January 10, 2022 - 7:37 am

and this works on domain servers. but it is not working on non-domain machines.
can you explain how to get this working to remote monitor on non domain servers? thanks

Reply
admin January 10, 2022 - 7:38 am

In order to use the Invoke-Command to connect to non-domain devices, you need to configure the WinRM (PowerShell remoting) with TrustedHost (or configure WinRM SSL – the harder way)

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Failed to Open the Group Policy Object on a Computer

    June 2, 2025
  • Remote Desktop Printing with RD Easy Print Redirection

    June 2, 2025
  • Disable the Lock Screen Widgets in Windows 11

    May 26, 2025
  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • Tracking and Analyzing Remote Desktop Connection Logs in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top