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.

gci -r| sort -descending -property length | select -first 10 name, length
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:

Subscribe to 4sysops newsletter!

gci -r|sort -descending -property length | select -first 10 name, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}}

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.

  1. RajeshN 5 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

  2. Rakesh Karia 5 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}

  4. Adel Soliman 4 years 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:

    $ComputerList = @(
    $ScriptBlock = {gci -Path C:\ -r| sort -descending -property length | select -first 10 name, length}
    Invoke-Command -ComputerName $ComputerList -ScriptBlock $ScripBlock
  6. Mrrrr 3 years ago

    This works a treat, thank you very much!

  7. Chetan 3 years ago

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

    • Leos Marek (Rank 4) 3 years ago

      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:

      gci -path C:\ -r|sort -descending -property length | select -first 10 name, @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}}


  8. Joe 3 years ago

    Recommend putting -ea SilentlyContinue after gci -r!

  9. How do we exclude gci to try to read certain subfolders? Seems like it goes through them all

    • You just need to use -include to include only those directories. But, if you have the possibility of directory names and file names being very similar to each other, you can expand this and get the list of directories to check, and then go through them.

      So, for example you could do something like this:

      get-childitem -path c:\topdir -recurse -directory -include test* | foreach-object { 
          get-childitem -path $_ -file | Select-Object -first 10 @{Name="Gigabytes";Expression={[Math]::round($_.length / 1GB, 2)}}

      You’d obviously want to massage it a lot more to get exactly what you want.. but that gives you the basis for what you’re looking for.

      David F.

  10. Vladi 1 year ago

    Thanks a lot for this tip. It helped a lot.

Leave a reply

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


© 4sysops 2006 - 2023


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


Log in with your credentials


Forgot your details?

Create Account