Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

May 28, 2021 PowerShellWindows 10Windows Server 2016

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 the 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
3
Facebook Twitter Google + Pinterest
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

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

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

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • 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
  • Configuring SFTP (SSH FTP) Server on Windows
  • Adding Drivers into VMWare ESXi Installation Image
  • How to Hide Installed Programs in Windows 10 and 11
Footer Logo

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


Back To Top