How To Use PowerShell to List Your 10 Largest Files

In this continuation of our series on building a PowerShell tool to track lost disk space, we’ll focus on adding a feature to identify and display the 10 largest files on the hard disk.

Brien Posey

December 16, 2024

4 Min Read
a visual representation of a hard disk being sorted into a list of the 10 largest files

In Part 1 of this series, I showed you how to build a GUI-based tree that displays a list of the folders on a hard disk. Let’s continue the discussion by going over the rest of the script.

As mentioned, this script uses a tree view to display all the folders on a specific hard disk. When you click on a folder, a pie chart reflects the size of that folder and its subfolders, comparing it to the system’s total disk space and remaining unused storage. The script also lists the 10 largest files on the hard disk. You can see the script in action in Figure 1.

the gui interface of the powershell tool

Figure 1. Here is what my script looks like.

Performance Considerations

You should know a few things before I explain how this script works.

  1. Speed Variation: The speed of this utility can vary significantly depending on the number of folders on the hard disk. The utility will run almost instantly if you have only a few folders. However, if you have many folders, processing them will take some time.

  2. GUI Responsiveness: When you have many folders, the GUI may initially appear to freeze, but it will eventually populate once the script has finished parsing all the folders.

  3. Pie Chart Update Time: The speed at which the pie chart refreshes when you click on a folder also corresponds to the number of folders. A few folders will result in an immediate update, while many folders may take several seconds.

Related:Building Graphical PowerShell Tools: A Three-Part Guide

Listing the Largest Files on the Hard Disk

With that said, let’s look at the part of the script that displays the list of the largest files on the hard disk. A single function handles this task, though the GUI elements are external to the function. The function, called Get-LargestFiles, is shown in the code below:


    Function Get-LargestFiles {

    $Files = Get-ChildItem -Path "$SelectedDrive\" -Recurse -File -ErrorAction SilentlyContinue

    # Sort the files by size and select the top 10
    $LargestFiles = $Files | Sort-Object Length -Descending | Select-Object -First 10

    # Format the result as a string
    $FileList = "Size `t`t File `r`n"
    ForEach ($File in $LargestFiles) {
        $FileList += "$([math]::Round($File.Length / 1GB, 2)) GB `t`t $($File.FullName) `r`n"
    }

    # Update the TextBox with the largest files
    $BigFiles.Text = $FileList
    $BigFiles.SelectionStart = 0
    $BigFiles.SelectionLength = 0
}

Breakdown of the Get-LargestFiles Function

The function starts by recursively executing the Get-ChildItem cmdlet to create a list of every file on the system. The command sets the error action to continue silently, allowing the script to ignore any folders it doesn’t have access to. The resulting list of files is stored in a variable called $Files. It’s important to note that the path is set using a variable called $SelectedDrive. The $SelectedDrive variable is defined at the beginning of the script and contains the drive letter for the hard disk the script will examine.

Identifying the largest files

The next step is to identify the 10 largest files. We use a single line of code:

Related:Use PowerShell to Provision Storage Within Windows Server

$LargestFiles = $Files | Sort-Object Length -Descending | Select-Object -First 10
  • The Sort-Object cmdlet sorts the list of files by size.

  • The Select-Object cmdlet narrows the list down to the 10 largest files. That top 10 list is written to a variable called $LargestFiles.

Formatting the output for the GUI

Next, we must format the list of files for use in the GUI. Since we are outputting the list to a text box, we must create a single string that includes the complete file list with the appropriate formatting. To accomplish this, I have set up a ForEach loop that processes each item in the list. For every file, the loop adds the file’s size (converted to gigabytes), path, and filename to a string called $FileList, with a couple of tabs and line breaks.

Displaying the results

The script then copies the formatted list to $BigFile.Text. $BigFile is the name of the GUI text box, effectively displaying the file list.

The last two lines of code

The final two lines of code set the text box’s selection start point and length to 0. Although these lines are usually unnecessary, I included them because Windows kept trying to automatically select the contents of my file list as I wrote the script. These commands prevent the automatic selection from occurring.

Now that I have shown you how to list of the 10 largest files, I will conclude the series in Part 3 by explaining how I created the pie chart.

Related:How to Check Disk Health and Identify Unhealthy Disks in Windows Server

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like