Use the Get-ChildItem cmdlet in PowerShell to get items in one or more specific locations and the file size attributes to find the large size files in the directory.
Most of the time as System administrators, we have to monitor file size in a specified location to find large-size files and take necessary action.
In this article, we will discuss how to use the PowerShell Get-ChildItem cmdlet to find large size files to take necessary actions.
PowerShell Find Large Size Files in Directory
Use the PowerShell Get-ChildItem cmdlet to find large size files in the current folder. Run below command
Get-ChildItem -File | Where-Object {$_.Length -gt 52428800} | Select Name, CreationTime,Length
In the above PowerShell script, the Get-ChildItem cmdlet gets file objects as specified by the -File parameter and passes its output to the second command.
The second command finds the file size larger than 50MB and passes its output to the third command.
The third command selects the file name, file creation time, and file size specified by Length and lists all files on the PowerShell console.
PowerShell Find Largest Files in Directory
To find the largest files in directory and subdirectories, use the Get-ChildItem cmdlet along with Sort-Object and Select-Object cmdlets in PowerShell.
The Get-ChildItem cmdlet retrives the files and folders within a directory.
The Sort-Object cmdlet sort the result based on the size or length of file.
The Select-Object cmdlet allows you to select the specific number of results such as biggest file in a directory or top 10 largest files in a folder.
Get-ChildItem -Path 'D:\PS' -File -Recurse | Sort-Object -Property Length -Descending | Select-Object -First 1
In the above PowerShell script, the Get-ChildItem command retrieves the files in a directory and subdirectories recursively specified by the Recurse pamarater and pipes the result to the Sort-Object cmdlet. The Sort-Object command sort the output by the Length property, which is the size of the file in bytes and pipes the result to the Select-Object cmdlet.
The Select-Obect command select the top 1 biggest files in a directory along with the name and size of the file.
The output of the above script to list biggest file is given below.
PS D:\> Get-ChildItem -Path 'D:\PS' -File -Recurse | Sort-Object -Property Length -Descending | Select-Object -First 1
Directory: D:\PS\CSV
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 17-08-2021 11:55 42916268 PowerShell-EventLogs.csv
PS D:\>
To get the top 10 largest files in a Windows directory, use the below command.
Get-ChildItem -Path 'D:\PS' -File -Recurse | Sort-Object -Property Length -Descending | Select-Object -First 10
The output of the above PowerShell script will retrives the top 10 largest files in a directory.
You can also use the above command with little modification in Path parameter to find large files on C drive, use the following command.
PowerShell Delete Files Size if Greater than Specific Size
Use Get-ChildItem to get items and find size greater than a specific size in the directory.
If the file size is greater than the specific size it will delete files using Remove-Item
Get-ChildItem -Recurse -File| Where-Object {$_.Length -gt 2775000} | ?{Remove-Item $_.fullname}
In the above PowerShell script, the Get-ChildItem cmdlet gets file object and passes its output to the second command.
The second command searches for a file size that is greater than the specific size ( 27775000 bytes) and passes its output to the third command.
The third command uses the Remove-Item cmdlet to delete large size files than a specific size.
Cool Tip: How to count files in the folder using Get-ChildItem in PowerShell!
Conclusion
I hope the above article on how to find large-size files in PowerShell using the Get-ChildItem cmdlet and delete file size larger than a specific size using Remove-Item
cmdlet.
Identifying a large files in a directory is important aspect of storage management. It helps to prevent the disk space issues, performance and assists in resource allocation. You can take better decision based on the large files whether to move file, archive or delete files to free up the space.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.
Recommended Content
Get File Version in PowerShell
Get File Attributes in PowerShell
Delete Files Older than x Days in PowerShell