Frequently, we get monitor alerts that the disk is under 20% full.

The question is usually what files are large that can be deleted. I often use the fantastic utility WinDirStat. And there are PowerShell commands to show you the top files on the disk.

But typically, some of the largest files are the paging file, or other system files that cannot be deleted. So I wanted a script to show me the largest of all the other files.

The Get-ChildItem does have an -Exclude parm, but I could’t figure out if it could take multiple directory names. So I broke down a script into 3 parts. First, get the array of files. Second, build a second array of files that are in all but the list of directories I wanted to exclude. Then third, sort that and select the Top 10 or 20 flies, then finally show the output (including the file size in Megabytes and the file creation date/time).

I’m not much of one for one-liner PowerShell commands. I would rather expand it out for more flexibility in the future.

Program Code

cls

$startingDirectory = "c:\"
$numFilesToShow = 20 

$fmtDateTime = $(get-date -f "MM/dd/yyyy HH:mm:ss")
Write-Host “Start: $fmtDateTime”

$files = Get-ChildItem $startingDirectory -file -recurse 
$files2 = @()

# eliminate any folders we can't do much about 
foreach ($file in $files) 
{
   # add any directory to exclude here (with another -or statement) 
   if (!($file.FullName.StartsWith("C:\Windows") -or $file.FullName.StartsWith("C:\Program Files") ) )
   {
      #note the "Not" sign in the if statement above,     
      #we keep in files Not in that list of directories 
      $files2 += $file
   }
} 

# Now sort after removing above folders 
$files3 = $files2 | sort-object Length  -descending | select-object -first $numFilesToShow


foreach ($file in $files3) 
{
   $size = $file.Length 
   $roundedSize = [Math]::Round($size)
   Write-Host "Name:$($file.FullName)  Length:$roundedSize MB  $($file.CreationTime)" 
} 


$fmtDateTime = $(get-date -f "MM/dd/yyyy HH:mm:ss")
Write-Host “END: $fmtDateTime" 

The program above could easily be enhanced to only return files over a given threshold (size).

Example Output

 
Start: 04/20/2020 10:20:22
Name:C:\inetpub\logs\LogFiles\FTPSVC2\u_ex200205.log  Length:2033873 MB  02/04/2020 18:01:07
Name:C:\inetpub\logs\LogFiles\FTPSVC2\u_ex200206.log  Length:1986952 MB  02/05/2020 18:04:29
Name:C:\inetpub\logs\LogFiles\FTPSVC2\u_ex200204.log  Length:1964928 MB  02/03/2020 18:00:19
Name:C:\inetpub\logs\LogFiles\FTPSVC2\u_ex200127.log  Length:1926786 MB  01/26/2020 18:01:43
Name:C:\inetpub\logs\LogFiles\FTPSVC2\u_ex200130.log  Length:1921223 MB  01/29/2020 18:03:02

So you can see from above, we have some log files from IIS/FTP that could potentially be deleted.

The above also demonstrates the following:
1) How to create an empty array in PowerShell
2) How to add an object to an existing array
3) How to round an number to the nearest integer in PowerShell (using the .NET Math.Round)
4) How to show the date/time a file was created (CreationTime)

Filed under: Powershell