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.

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.

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:
- Read JSON File into Array in PowerShell
- Delete File If Exists In PowerShell
- How to Check if a Folder is Empty in PowerShell?
- How to Split Large Text Files with PowerShell?
- Get File Name Without Extension in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.