Prof. Powershell

The PowerShell File Frontier, Part 2: File Length

Here's how to find specific files by their size.

More on this topic:

In my previous lesson we looked at a way to copy files between computers. Although you could just as easily copy to a different local location or use Move-Item which will copy and then delete the file. Yes, I realize there are tools like Robocopy that you could also use but this is Prof. PowerShell not Prof. Robocopy and my aim is to instruct as much as it is to provide practical solutions.

Another file need you might have is to identify files that might be larger or smaller than a certain size and then do something with that information. In PowerShell, file objects have a property called Length which is the size of the file in bytes. I'm an old-school IT Pro and I don't think of files by "length" I think about their "size", but you simply have to adjust. As with finding files modified after a certain date, you can also find files by size. You will need to pipe the files to Where-Object and filter.

PS C:\> dir c:\scripts -file | where {$_.length -ge 1mb}

Directory: C:\scripts

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          1/8/2008   2:16 PM    1051136 DataGridView FAQ.doc
-a---         2/18/2014   2:06 PM    3284932 engineering.xml
-a---          6/1/2010   5:43 PM    3107965 lastlist.txt
-a---          2/8/2011  11:21 AM    1568444 LastNames.txt
-a---         2/25/2011   9:27 AM    1056768 secedit.sdb
-a---          5/8/2014   3:32 PM   12422302 cmdlets.xml
-a---          1/4/2013   3:04 PM    4346547 WMIDiag.vbs

In PowerShell v3 and later you could also write the expression like this:

PS C:\> dir c:\scripts -file | where length -ge 1mb

Because you get the file object at the end of the pipeline you could do whatever you want. Perhaps you want to delete the file.

PS C:\> dir c:\scripts -file | where length -ge 1mb | del –whatif

The del command is an alias for Remove-Item and one you are probably familiar with from the CMD shell. You could also copy or move the files.

However if you want a complex filtering, then you'll need to revert back to a legacy approach.

PS C:\> dir c:\scripts -file | where {$_.lastwritetime -ge "8/1/2014" -AND $_.length -ge 1KB}

If there are files that match the filtering criteria, they will be written to the pipeline and I can do whatever I need to with them.

Finally, you may simply need to find 0 byte files and delete them.

PS C:\> dir c:\scripts -file -Recurse | where {$_.length -eq 0} | del -whatif
What if: Performing the operation "Remove File" on target "C:\scripts\MaximumSize".
What if: Performing the operation "Remove File" on target "C:\scripts\quark\empty.txt".

If you need to run any of these commands against folders on a remote server, I recommend using a remoting technique like Invoke-Command so that the Get-ChildItem command runs on the remote machine and you don't push a lot of SMB traffic over the network.

You might be thinking this is a lot of work and typing to remember. Next time I'll share a solution with you.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular