How to Check if a File is Empty with PowerShell?

Are you looking to check if a file is empty or not in PowerShell? In this PowerShell tutorial, I will show you different methods to check if a file is empty with PowerShell.

To check if a file is empty in PowerShell, you can use the Get-Item cmdlet to retrieve the file’s properties without loading its content. If the Length property of the file is zero, then the file is empty. Here’s a concise example:

if ((Get-Item 'C:\MyFolder\MyFile.txt').length -eq 0) {
    "The file is empty."
} else {
    "The file has content."
}

This method is quick and efficient, especially for larger files, as it does not read the file’s content into memory.

Using the Get-Content Cmdlet

One of the simplest ways to check if a file is empty in PowerShell is by using the Get-Content cmdlet. This cmdlet reads the content of a file and can be used to measure its length. If the length is zero, the file is empty.

if ((Get-Content -Path C:\MyFolder\MyFile.txt).length -eq 0) {
    Write-Host "The file is empty."
} else {
    Write-Host "The file has content."
}

You can see in the screenshot below that I executed the PowerShell script using VS code.

Check if a File is Empty with PowerShell

Using the Get-Item Cmdlet

A more efficient way to check for an empty file in PowerShell is to use the Get-Item cmdlet. This cmdlet retrieves the file object, which includes properties such as the file length, without reading the file content.

if ((Get-Item 'C:\MyFolder\MyFile.txt').length -eq 0) {
    Write-Host "The file is empty."
} else {
    Write-Host "The file has content."
}

This method is faster and more resource-friendly, particularly for larger files. It checks the file’s length property directly from the file system metadata.

You can see in the screenshot below I executed the PowerShell script.

powershell Check if a File is Empty

Using the Test-Path Cmdlet

While the Test-Path cmdlet is typically used to verify the existence of a file or directory, it does not directly check if a file is empty.

if (Test-Path 'C:\MyFolder\MyFile.txt') {
    # Additional checks for file content would be needed here
}

Using the [System.IO.FileInfo] Class

PowerShell can also access .NET classes directly, such as [System.IO.FileInfo], to check file properties.

$fileInfo = New-Object System.IO.FileInfo 'C:\MyFolder\MyFile.txt'
if ($fileInfo.Length -eq 0) {
    Write-Host "The file is empty."
} else {
    Write-Host "The file has content."
}

This method utilizes the .NET framework’s file handling capabilities and is a robust way to check file properties.

Using the [string]::IsNullOrWhiteSpace() Method

Another method to check if a file is empty or contains only whitespace is by using the [string]::IsNullOrWhiteSpace() method in PowerShell. This method can be particularly useful if you consider a file with only whitespace as empty.

$content = Get-Content 'C:\MyFolder\MyFile.txt' -Raw
if ([string]::IsNullOrWhiteSpace($content)) {
    Write-Host "The file is empty or contains only whitespace."
} else {
    Write-Host "The file has content."
}

This method reads the entire file content as a single string and checks if it is null, empty, or consists solely of whitespace characters.

Conclusion

I hope now you have an idea of how to check if a file is empty using PowerShell using various methods like Get-Item, Get-Content cmdlet, etc.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.