# Script to check available disk space on Windows

Low disk space on a device is a significant issue that can negatively impact the performance and productivity of the endpoints. Administrators would want to keep track of the available disk space on a device to check if a required amount of space is present, for instance, to push major OS updates or other company files/resources. Close monitoring of disk spaces is sometimes required to identify any disks that might be clogged with unwanted cache and files. Hexnode UEM lets admins check for the available disk space on Windows devices using [custom scripts](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/).

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

Batch script
------------

Batch script to fetch the available disk space

wmic logicaldisk get freespace @echo off SetLocal EnableDelayedExpansion set count=0 for /F "delims=" %%a in ('wmic logicaldisk get freespace') do ( set Zone=%%a set /a count=!count! + 1 if !count! GTR 1 goto Exit ) :Exit echo wsh.echo cdbl(%Zone%)/1073741824 > %temp%.\\tmp.vbs for /f %%a in ('cscript //nologo %temp%.\\tmp.vbs') do set Zone=%%a del %temp%.\\tmp.vbs echo Available Disk Space- %Zone% GB 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  wmic logicaldisk get freespace

@echo off 

SetLocal EnableDelayedExpansion 

set count=0

for /F "delims=" %%a in ('wmic logicaldisk get freespace') do (

 set Zone=%%a

 set /a count=!count! + 1

 if !count! GTR 1 goto Exit

)

:Exit 

echo wsh.echo cdbl(%Zone%)/1073741824 > %temp%.\\tmp.vbs 

 for /f %%a in ('cscript //nologo %temp%.\\tmp.vbs') do set Zone=%%a

 del %temp%.\\tmp.vbs 

 echo Available Disk Space- %Zone% GB 

   

 

  

![Displays the available space in the memory drive](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/01/Batch-Script-to-find-available-disk-space-on-Windows.png "Batch script to fetch the available disk space on Windows")[](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2023/01/Batch-Script-to-find-available-disk-space-on-Windows.png)

This script displays the available disk space in GB.

### PowerShell script

PowerShell script to check available disk space

$disks = Get-WmiObject Win32\_LogicalDisk -Filter "DriveType = 3" foreach ($disk in $disks) { $freeSpace = "{0:N2}" -f ($disk.FreeSpace/1GB) $totalSpace = "{0:N2}" -f ($disk.Size/1GB) $usedSpace = "{0:N2}" -f ($totalSpace - $freeSpace) $percentFree = "{0:N2}" -f (($freeSpace / $totalSpace) \* 100) Write-Host "Drive $($disk.DeviceID) has $freeSpace GB free of $totalSpace GB, $usedSpace GB used, $percentFree % free" } 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

  $disks = Get-WmiObject Win32\_LogicalDisk -Filter "DriveType = 3"

foreach ($disk in $disks) {

 $freeSpace = "{0:N2}" -f ($disk.FreeSpace/1GB)

 $totalSpace = "{0:N2}" -f ($disk.Size/1GB)

 $usedSpace = "{0:N2}" -f ($totalSpace - $freeSpace)

 $percentFree = "{0:N2}" -f (($freeSpace / $totalSpace) \* 100)

 Write-Host "Drive $($disk.DeviceID) has $freeSpace GB free of $totalSpace GB, $usedSpace GB used, $percentFree % free"

} 

   

 

  

![List of all drives and their available space ](https:2023/01/PowerShell-script-to-fetch-the-available-disk-space-on-Windows.png "PowerShell script to fetch the available disk space on Windows")[](https:2023/01/PowerShell-script-to-fetch-the-available-disk-space-on-Windows.png)

This script displays the drive name, available disk space, used space, and the percentage of free space available.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.