How to get empty files in Windows OS using PowerShell?


To get the list of empty files in Windows OS using PowerShell, there are two ways,

a) Using Length parameter. We will count the length of the file. If it is 0 then the file is empty as shown below.

Get-ChildItem C:\Temp\ -Recurse | where{$_.Length -eq 0} |
Select @{N='EmptyFiles';E={$_.FullName}}

Output:

b) Another method is a long one that we don’t want to go into. We need to check each file's content and if it is empty then we will declare that file as an empty file.

Get-ChildItem C:\Temp -Recurse -File | foreach{
   if((Get-Content $_.FullName) -eq $null){
      $_.FullName
   }

This method is quite slow because it scans the entire content of each file. It is always better to use the first method.

Updated on: 30-Mar-2021

874 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements