Udostępnij za pośrednictwem


Counting Public Folder SubFolders

This sample script was written by a brilliant fellow PFE named Chris Schrimsher.  Chris gave me permission to post this script out there to the two of you reading my posts…you know who you are :)

In this script there is a concept that he is using that I like.  He uses a hash table (dictionary object), to build up a list of folders and their count of subfolders.  As he works through the entire tree of public folders, he just checks to see if the parent is in the list.  If its there, he just adds to the number count, if not he adds it with a 1.  Its a very simple but brilliant use of a hash table.

I saw some of this script when he was writing it, and I must confess, I haven't actually run this version I am posting here.  He prettied it up a bit and is also using a progress bar here.

Well, enough of me wasting time, here is the sample code that you can pull lots of neat scripting concepts from:

 # Name:   Get-PFSubFolderCount.ps1# Author: Chris Schrimsher# Date:   August 6, 2009## Description:# Enumerates all public folders, and generates a count of the number of subfolders that each folder has.# Warns about any folders that have more than the maximum specified below, and makes full report available.

This script is provided "AS IS" with no warranties, and confers no rights.# Use of any portion or all of this script are subject to the terms specified at# https://www.microsoft.com/info/cpyright.htm.

$MaxSubFolders = 250

Clear-Host

Populate the initial progress bar (real time progress updating is not available while retrieving public folder information)

Write-Progress -Activity "Public Folder Subfolder Count Tool" -CurrentOperation "Enumerating Public Folders, Please Wait..." -Status "This may take some time, especially in large organizations..."

Get all public folders and store in variable

$PubFolders = Get-PublicFolder -Recurse -ResultSize Unlimited# Create hash table to hold results $SubFolderTable = @{}# Write-Host "Analyzing Public Folders and Building Results Table..."

Create a counter variable

$Count = 0

Enumerate through public folders one at a time

$PubFolders | ForEach-Object { # Update the progress bar as each public folder is analyzed Write-Progress -Activity "Public Folder Subfolder Count Tool" -CurrentOperation "Analyzing Public Folders and Building Results Table..." -Status "Completion Status:" -PercentComplete ([int] (($Count / $PubFolders.Count) * 100)) # If the parent path is null, this is the root folder, so skip it if ($.ParentPath -ne $null) { # If the parent path is not contained in the hash table, ... if ($SubFolderTable[$_.ParentPath] -eq $null) { # ... add it with a value of 1 (since this is the first subfolder of that parent) $SubFolderTable[$.ParentPath] = 1 } else { # ... otherwise, increment the count of the existing parent path. $SubFolderTable[$_.ParentPath]++ } } # Increment the counter variable $Count++}Write-Progress -Activity "Public Folder Subfolder Count Tool" -CurrentOperation "Counting folders with more than $MaxSubFolders subfolders..." ` -Status "Please wait..."

Write-Host "Counting folders with more than $MaxSubFolders subfolders..."

$LargeFoldersCount = ($SubFolderTable.GetEnumerator() | Where-Object { $_.Value -gt $MaxSubFolders } | Measure-Object).Countif ($LargeFoldersCount -gt 0){ # Display count of folders with too many subfolders Write-Host "Number of folders with more than $MaxSubFolders subfolders: $LargeFoldersCount" # Display help information for table Write-Host "nList of folders with more than $MaxSubfolders subfolders and subfolder count of each:" # Display hash table containing results sorted by number of subfolders, from largest to smallest $SubFolderTable.GetEnumerator() | Where-Object { $_.Value -gt $MaxSubFolders } | Sort-Object -Property Value -Descending | Format-Table # Display count of folders with too many subfolders Write-Host "nNumber of folders with more than $MaxSubFolders subfolders: $LargeFoldersCount" Write-Host "nFull output is available in the object $SubFolderTable.n" }else{ # Display a message indicating no folders with too many subfolders were found Write-Host "nNo folders found containing more than $MaxSubFolders subfolders."}

-Gary

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.

Comments

  • Anonymous
    September 26, 2015
    I started counting subfolders on one of our large public folders and gave up at around 300 noting that I was only about a third of the way through so there should be something like 900 folders This script thinks the folder has 206 subfolders! Have I misunderstood the purpose of this script or something?