How to get empty files in Windows OS using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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.

raja
Published on 30-Mar-2021 13:04:56
Advertisements