Powershell Function to Get Largest Files on a Drive or in a Folder

Ofentimes when a disk space shortage alert pops up from a specific server, I’d like to know quickly which files are the largest that I might have a way to shrink or move or delete. Here are the two funtions that I use to immediately locate the full list of top N largest files at a specific location (be it a hard drive on a server or a UNC path):

[sourcecode language=”powershell”]
#Function to get the largest N files on a specific computer’s drive
Function Get-LargestFilesOnDrive
{
Param([String]$ComputerName = $env:COMPUTERNAME,[Char]$Drive = ‘C’, [Int]$Top = 10)
Get-ChildItem -Path \\$ComputerName\$Drive$ -Recurse | Select-Object Name, @{Label=’SizeMB’; Expression={"{0:N0}" -f ($_.Length/1MB)}} , DirectoryName, Length | Sort-Object Length -Descending | Select-Object Name, DirectoryName, SizeMB -First $Top | Format-Table -AutoSize -Wrap
}

#Function to get the largest N files on a specific UNC path and its sub-paths
Function Get-LargestFilesOnPath
{
Param([String]$Path = ‘.\’, [Int]$Top = 10)
Get-ChildItem -Path $Path -Recurse | Select-Object Name, @{Label=’SizeMB’; Expression={"{0:N0}" -f ($_.Length/1MB)}} , DirectoryName, Length | Sort-Object Length -Descending | Select-Object Name, DirectoryName, SizeMB -First $Top | Format-Table -AutoSize -Wrap
}
[/sourcecode]


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *