Find files of given size using PowerShell


Get-ChildItem

The cmdlet can be used to get the items and the child items in a  particular location, folder or a directory.

Get-ChildItem [[-Path] <String[]>]

In order to get the files in a folder or subfolder, you can use this cmdlet.

To traverse the sub folders, you can use -Recurse parameter and to limit the number of levels that needs to be recursed, you can use -Depth parameter.

To get the list of files greater than, less than or equal to, you can use Where-Object. It allows to select an object from a collection based on some condition.

Get-ChildItem [path] -recurse | where-object {$_.length -gt [size]} | Sort-Object length

Example

Get-ChildItem ‘C:\Users\ImgToUpload1 – Copy’ -recurse | where-object {$_.length -gt 2000000} | Sort-Object length

This will list all the files in ‘C:\Users\ImgToUpload1 – Copy’ folder that are greater than 2MB in size and also sort them based on the size or length in ascending order (default order). In order to sort the list in descending order, you can use -Descending.

Example

Get-ChildItem ‘C:\Users\ImgToUpload1 – Copy’ -recurse | where-object {$_.length -gt 2MB} | Sort-Object length -Descending

You can use

  • -lt for less than
  • -eq for equal to
  • -ne for not equal to
  • -ge for greater than or equal to
  • -le for less than or equal to

Note: ‘=’ sign is not used to check equality in PowerShell as it is used as an assignment operator

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.