Mason Prewett, Business Intelligence at Key2 Consulting
By: Mason Prewett


 

Introduction

As an admin of many server types, I often encounter the issue of a machine running out of disk space. Tracking the culprit folder that was hogging up disk space used to be a troublesome, manual effort. However, I recently coded a way to speed this process up with PowerShell. No more clicking folder by folder, inspecting their properties, and determining folder size one by one. With a bit of PowerShell code (that I’ll share with you) and an HTML file, I’ll demonstrate how to create a tree view of your machine’s file structure – with a size breakdown included – so you can quickly find your space hog.
 

How It Works

Part 1: The PowerShell Code

The PowerShell code below works by dynamically generating html that will produce the file directory tree view hierarchy. In order to do this, it is necessary to recursively (more detail on recursion) go through the folder structure and generate the html per folder.

Once the html is produced, it will be inserted into an html file template that is covered in the next section. When complete, you can simply open the html file to view the results! With a quick scan of the file tree view, you’ll be able to see exactly which file(s) is taking up space.

Once you have the HTML file in place and you’re ready to try this, make sure to set the variables at the top of the below PowerShell code to accommodate your configuration. You can set the starting folder location to analyze, which will help you avoid returning too much unnecessary information.

 

Part 2: The HTML Template

The HTML file serves as the template for the dynamically generated HTML code to be inserted into. This file is a dependency and MUST be present in order for the above script to succeed!

The template string that is located in the file is [FinalHTML]. If you wish to change this template string, you will need to update this line of code as well:

Note: The tree view html/JavaScript/css code was pulled from W3 Schools.

HTML Template Code (Create an html file and paste this in to it):

 

What the End Result Looks Like


 

Notes

  1. You may notice that the subfolder sizes do not always add up to the total size displayed of their parent. This is because the folder sizes include files that are in the folders, but the html file only displays the folder and subfolder breakdowns.
  2. The tree view html/JavaScript/css code was used from this W3 Schools tutorial.

 

Questions?

Thanks for reading! We hope you found this blog post to be useful. Do let us know if you have any questions or topic ideas related to BI, analytics, the cloud, machine learning, SQL Server, (Star Wars), or anything else of the like that you’d like us to write about. Simply leave us a comment below, and we’ll see what we can do!
 

Keep your data analytics sharp by subscribing to our mailing list

Get fresh Key2 content around Business Intelligence, Data Warehousing, Analytics, and more delivered right to your inbox!

 
 
 


Key2 Consulting is a data warehousing and business intelligence company located in Atlanta, Georgia. We create and deliver custom data warehouse solutions, business intelligence solutions, and custom applications.