Skip to main content

Command Palette

Search for a command to run...

How to compare file lengths with PowerShell

Updated
1 min read
How to compare file lengths with PowerShell

After a file copy is complete, how can you be sure the entire file was copied? This can be done with the file length command in PowerShell. This will compare the length in bytes of each file and, combined with an if statement, is a way to perform remediation on a failed file copy automatically.

First, store each file contents in memory using variables and the Get-Item command. Then compare them with an if statement using the file.Length command. The space in the else rackets can be used to take appropriate action if the file lengths do not match.

$file1 = Get-Item ‘C:\FilePath\File1.file’ -Verbose
$file2 = Get-Item ‘C:\FilePath\File2.file’ -Verbose
If ($file1.Length -eq $file2.Length)
{ Write-Host “File Sizes Match” }
else
{ Write-Host “File Sizes Do Not Match” }

More from this blog

Unemployed Philosopher

15 posts

I am a teacher, a writer, and a dad. I enjoy learning software and writing about my experience. I also love baseball, cooking, and reading old books.