Find Files Larger than a Specific Size with PowerShell & CMD

Print Page

In this post I will show you how to find files that are larger than a specific size in a folder and all its sub-subfolders. My choice of command line utilities for this operation is going to be the built-in PowerShell and Command Prompt.

Windows PowerShell

The following command will find and list all files that are larger than 500MB in the entire C:\ drive.

Get-ChildItem C:\ -recurse | where-object {$_.length -gt 524288000} | Sort-Object length | ft fullname, length -auto

Explanation:

  • -recurse parameter is used to find files stored in all sub-directories recursively. To find files only in the main folder you can remove this parameter.
  • where-object {$_.length -gt 524288000} is our filter. It collects only those files that are greater than 500MB, while ignoring the rest. Size is in bytes. If you’d like to do the opposite, i.e., find files smaller than 500MB, then use the -lt parameter.
  • Sort-Object length sorts result in ascending (default) order by length. To sort in descending order use this: Sort-Object length -descending.
  • ft fullname, length -auto: ft or Format-Table is used to format the result. -auto prevents the output from spreading to the full width of the window, and consequently making the result easier to read. Run get-childitem | get-member to see other properties.

If you’re interested only in a specific file type, specify it this way in the command:

Get-ChildItem C:\ -recurse -include *.exe

Windows Command Prompt

In Command Prompt, forfiles command is used for batch processing a command on a file or set of files. The following command will find and list all files that are larger than 500MB in the C:\ drive.

forfiles /P C:\ /M *.* /S /C "CMD /C if @fsize gtr 524288000 echo @PATH @FSIZE"

Explanation:

  • /P (Path): Specifies the path to process. If ignored, process starts in the current working directory.
  • /M (Mask): Specifies the search filter. Example, for executables use *.exe.
  • /S (Sub-Directories): Search for files in sub-directories recursively.
  • /C (Command): Runs the specified command enclosed in quotation marks on each file.
  • @PATH: Is a variable. It shows the full path of the file.
  • @FSIZE: Shows the file size, in bytes.

To see list of other parameters and variables, simply enter forfiles /? in the command-line. This will help you to expand and format the output.