Getting Top Level Folder Report in PowerShell

One of the sessions I presented recently at TechDays San Francisco was on file share management with PowerShell. One of the scripts I demonstrated was for a function to get information for top level folders. This is the type of thing that could be handy to run say against the root of your shared users folder. Or the root of a group share where each subfolder is a share that belongs to a different group. My function takes advantage of a new feature for Get-ChildItem that makes it much easier to retrieve only file or directories. Here’s my Get-FolderSize function.

The function defaults to the local path and gets a collection of all of the top level folders, that is, those found directly in the root. The function then takes the collection of folders and pipes them to ForEach-Object. Most of the time we only use the Process scriptblock with ForEach-Object, but I want to take advantage of the Begin and End blocks as well. In the Begin scriptblock I measure all of the files in the root of the parent path and create a custom object that shows the number of files and total size in bytes. I’m going to get this same information for each child folder as well.

The process scriptblock does just that for each top level folder. This version of my function uses Write-Progress to display progress and in the End script block I have code to complete the progress bar, although It works just fine without it.

Other techniques I’d like to point out are the use of splatting and error handling. You’ll notice that I’m using the common -ErrorVariable parameter. After exploring the different types of exceptions I decided I could easily display any errors and the paths In the Catch block. I’m using Write-Warning, but this could just as easily be written to a text file.

The function writes an object like this for every folder.

Here’s an example of complete output:

foldersizeBecause I’ve written objects to the pipeline, I could pipe this to Out-Gridview, export to a CSV file or create an HTML report.

This is just a taste of what you can accomplish with some basic PowerShell commands.


5 thoughts on “Getting Top Level Folder Report in PowerShell

  1. Is there a reason you wouldn’t use (Get-ChildItem -Recure -Force | Where {$_.psiscontainer -eq $false | Measure-object -sum -Property Length).Sum with a custom object? Does it run faster than Get-ChildItem in version 2.0? I typically run into path length limitations based on ridiculous user folder creation with deep paths.

    • The main reason is that I wanted to take advantage of the new parameters for Get-Childitem in v3. I haven’t tried it on anything with a really deep path structure.

      • In v2, getting just folders using the where clause on my Scripts directory took 2.95 seconds. In v3 using the new -Directory took 148ms. Doing the same for files took 609MS in 2.0 and 534MS in 3.0. I then tested using C:Windows to get files. PowerShell 2.0 took 94.25 seconds and PowerShell 3.0 took 31.15 seconds.

  2. Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #28 - TechCenter - Blog - TechCenter – Dell Community

Comments are closed.