To find large files on a computer, you can use Windows File Explorer, one of the third-party tools (such as WinDirStat or TreeSize), or your favorite file manager. In some cases, it is more convenient to use PowerShell commands to quickly find large files on a computer because there is no need to install third-party tools.
For example, to list the 10 largest files on a particular drive, run the following PowerShell command
Get-ChildItem -Path c:\ -Recurse –Force| sort -descending -property length | select -first 10 FullName, Length
After some time (depending on the size of the partition and the number of files), the command will return a list of the 10 largest files, sorted by size.
The Get-ChildItem cmdlet is used to get a list of files in a directory (or you can use one of the aliases: ls
, dir
or gci
). The following parameters are used in the above command to find large files::
-Path C:\
— path to the directory to search files. You can perform a full disk search or a folder search (for example, in a user profile or shared folder). If the path is not specified, the current directory is searched. The -Path parameter allows to specify multiple folders and drive letters to search (separated by commas):-Path C:\,G:\,D:\IS0,F:\Downloads
-Recurse
– this option indicates that a recursive search should be performed in all subdirectories. To restrict the folder search to a certain depth level, use the -Depth parameter.-Force
– option is used to include hidden folders and system folders (files) in the search (which are ignored by default).Sort -descending -property Length
– sort files by size in descending order (by the Length property).Select FullName, Length
– show the full path to the file and its size.-first 10
– list the first 10 files only.
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
Add the -ErrorAction SilentlyContinue parameter to the Get-ChildItem command to suppress these errors.
By default, the Get-ChildItem cmdlet displays file sizes in bytes. Change the command as follows to convert them to gigabytes (GB):
Get-ChildItem c:\ -r -Force -ErrorAction SilentlyContinue |sort -descending -property length | select -first 10 name, DirectoryName, @{Name="GB";Expression={[Math]::round($_.length / 1GB, 2)}}
The resulting list of large files can be converted into a convenient graphical table using the Out-GridView cmdlet:
Get-ChildItem c:\ -r -Force -ErrorAction SilentlyContinue |sort -descending -property length | select -first 10 name, DirectoryName, @{Name="GB";Expression={[Math]::round($_.length / 1GB, 2)}} | Out-GridView
Find all files on a hard drive larger than 10GB and export the list to a CSV file:
GCi C:\ -Recurse -Force -ErrorAction SilentlyContinue | where-object {$_.length -gt 10GB} | Sort-Object length | ft fullname,length| Export-Csv c:\pc\LargeFiles_Report.csv
Certain file types can be included or excluded from the search scope using the -Include and -Exclude options. For example, exclude files with SYS
and VMDK
extensions from the output:
Get-ChildItem G:\ -Exclude "*.sys","*.vmdk" -Recurse -Force -ErrorAction SilentlyContinue| sort -descending -property length | select -first 10 FullName, Length
To retrieve only files that match some filter criteria, use the -Filter option. For example, -Filter "*image*"
.
You can search for large files on a remote computer. In this case, specify the UNC path to a shared folder as the Get-ChildItem parameter (it is possible to specify the UNC path to the admin shares C$, D$ on a remote computer):
Get-ChildItem -Path \\PC123\C$ -Recurse |where-object {$_.length -gt 10GB} |sort -descending -property length | select -first 10 FullName, Length
Or, use the Invoke-Command cmdlet to get a list of the largest files on multiple computers:
$ComputerList = @("server1","server2")
Invoke-Command -ComputerName $ComputerList -ScriptBlock {gci -Path C:\ -r| sort -descending -property length | select -first 10 name, length}
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.