Finding large files with PowerShell

If your hard drive is running out of space, you'll want to know if some big chunks you no longer need are consuming too many disk resources. A few free tools exist for this purpose; however, thanks to PowerShell, you don't really need to install them.

Listing files ordered by size ^

The starting point is the cmdlet Get-ChildItem that allows you to list files and directories. Get-ChildItem alone can't find the largest files. Thus, you have to filter its output to extract the wanted properties.

Sorting files by size

Sorting files by size

If you want to examine entire directory trees, you have to use Get-ChildItem recursively by adding the -r switch. Then you sort the collection of FileInfo objects in descending order based on the Length property (sort as an alias for Sort-Object). And with the help of Select-Object (alias select), you pick the first 10 entries with their names and sizes.

Displaying file sizes in GB ^

The result has a little cosmetic flaw: it displays the file sizes in bytes. Particularly with GB-sized chunks, you will get very big numbers. This makes it difficult to get an overview. You can solve this problem by calculating the corresponding property:

Instead of displaying the property length unprocessed, you can convert file sizes with a calculated property to GB and round the numerous decimal places with the round function.

9+
avataravataravataravatar

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

9 Comments
  1. RajeshN 3 years ago

    Awesome article! Thanks much for enlightening us about the best tools to find the large files with PowerShell. Also, you've delivered the great & helpful tips in this article i.e. how to sort the files by size and how to display the file sizes in GB. Great information, thank you!

    Best Regards
    RajeshN, Nous Infosystems

    3+
    avatar
  2. Rakesh Karia 2 years ago

    How to delete them as well ?

    1+

  3. Just add the FullName property to the Select-Object cmdlet and pipe the result to the Remove-Item cmdlet via the Foreach-Object cmdlet.

    This means you just have to add the following (don't forget the starting comma) to one of the two examples provided by Wolfgang.

    ,FullName|ForEach-Object -Process {Remove-Item -Path $_.FullName}

    4+
    avatar
  4. Adel Soliman 2 years ago

    Hi,

    Very good script. What if I want to display a remote PC file size. Or how can I run this command on a remote pc.

    Thanks

    0

  5. @Adel Soliman,

    One possibilty is to use the Invoke-Command cmdlet.

    For example:

    4+
    avatar
  6. Mrrrr 10 months ago

    This works a treat, thank you very much!

    0

  7. Chetan 9 months ago

    when opening powershell, it is on H:\ drive.. how do I change it to different drive to check the space

    0

    • The command is executed at your current prompt location. Simply change the drive with C: or other drive letter command as usual. 

      Or, put it directly to the command:

       

      0

  8. Joe 9 months ago

    Recommend putting -ea SilentlyContinue after gci -r!

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account