- Delegate permissions for domain join - Mon, Jun 5 2023
- Join Windows 11 to an Active Directory domain - Thu, Jun 1 2023
- Change Windows network profiles between public and private - Wed, May 24 2023
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
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.
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
How to delete them as well ?
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}
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
@Adel Soliman,
One possibilty is to use the Invoke-Command cmdlet.
For example:
This works a treat, thank you very much!
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:
Recommend putting -ea SilentlyContinue after gci -r!
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:
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.
Thank you David for taking the time out to respond. I’ll give it a try
Thanks a lot for this tip. It helped a lot.