When the system warns you that free space on your local drive is running out, the first thing that the administrator does is trying to find all large files that occupy much space. To search for new files, you can use Windows Explorer (there are several pre-defined templates of searching by size), your favorite file manager or third-party tools. However, unlike PowerShell, all these tools require installation. Let’s consider the example of quick searching large files on your local computer drive using PowerShell.
You can use the Get-ChildItem cmdlet to list the files in a specific directory (including subfolders) and their sizes.The cmdlet can search files across the entire disk or in a specific folder (for example, in user profiles and any other folders).
Let’s list the 10 largest files on disk C:\:
Get-ChildItem c:\ -r| sort -descending -property length | select -first 10 name, Length
Depending on the disk size and the number of files on it, it may take some time to complete the command.
As you can see, we got the list of ten largest files on the disk sorted in the descending order.
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem c:\ -r| sort -descending -property length | select -fir ...
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
To suppress such errors, use the -ErrorAction SilentlyContinue parameter.
Use the -Force option to display hidden and system files that are not accessible to the user.
As you can see, the file size is displayed in bytes. For convenience, they can be converted into megabytes. You can also display the folder, in which the found file is stored:
Get-ChildItem c:\ -r -ErrorAction SilentlyContinue –Force |sort -descending -property length | select -first 10 name, DirectoryName, @{Name="MB";Expression={[Math]::round($_.length / 1MB, 2)}}
The resulting table can be converted into a convenient graphic table using the Out-GridView cmdlet:
Get-ChildItem c:\ -r|sort -descending -property length | select -first 10 name, DirectoryName, @{Name="MB";Expression={[Math]::round($_.length / 1MB, 2)}} | Out-GridView
Similarly, you can find all files that are larger than a certain size, for example, 500 MB:
$size=500*1024*1024
GCi C:\ -recurse -ErrorAction SilentlyContinue –Force | where-object {$_.length -gt $size} | Sort-Object length | ft fullname
You can export the list of files into a CSV file as follows:
GCi C:\ -recurse | where-object {$_.length -gt $size} | Sort-Object length | ft fullname | Export-Csv c:\pc\LargeFiles_Report.csv
If you need to calculate the size of all files in a specific directory, read the article Calculating Folder Size with PowerShell.
2 comments
You have a typo in the code GCi C:\ -recurse -ErrorAction –Force SilentlyContinue. The -Force should be after SilentlyContinue
Thank you mate. I will fix it.