Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links


SQL Server DBA Jobs

Powershell script to compare two file sizes

18 June,2012 by Tom Collins

Question: I have a nightly ETL , which requires some file manipulation.  The requirement is to a) compare the file lengths  b) if the file is bigger – backup the original file c) Replace the new original file with the new file .

 How can I achieve this in Powershell?

 Answer:  This powershell script uses Get-item to compare two file sizes , backup file 2 and force a replace if bigger

 $file1 = Get-Item 'C:\projects\testing\file1.txt'
$file2 = Get-Item 'C:\projects\testing\file2.txt'
If ($file1.Length -gt $file2.Length)
Write-Host "The file is bigger"
#backup copy of file on another folder
Copy-Item $file2 -Destination 'C:\projects\testing\removeItem'
Copy-Item $file1 -Destination $file2 -Force


Author: Tom Collins (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on Powershell script to compare two file sizes | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer