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):

#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
}
Advertisements

About Nick Xu

Not a DBA anymore
This entry was posted in Powershell and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s