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.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Users who have LIKED this post:

  • avatar
  • avatar
  • avatar
  • avatar
  1. RajeshN 2 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


    Users who have LIKED this comment:

    • avatar
  2. Rakesh Karia 2 years ago

    How to delete them as well ?


  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}


    Users who have LIKED this comment:

    • avatar
  4. Adel Soliman 1 year ago


    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.



  5. @Adel Soliman,

    One possibilty is to use the Invoke-Command cmdlet.

    For example:


    Users who have LIKED this comment:

    • avatar
  6. Mrrrr 2 months ago

    This works a treat, thank you very much!


  7. Chetan 4 weeks ago

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


    • 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:



  8. Joe 2 weeks ago

    Recommend putting -ea SilentlyContinue after gci -r!


Leave a reply

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


© 4sysops 2006 - 2020


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


Log in with your credentials


Forgot your details?

Create Account