Use the Get-ChildItem cmdlet in PowerShell to get items in one or more specific locations and the file size attributes to find large-size files.
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
Use 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 file size larger than 50MB and passes its output to the third command.
The third command selects file name, file creation time, and file size specified by Length and lists all files on the PowerShell console.
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 search for 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 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.
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