Remove Empty Public Folders in 3 Lines of PowerShell

I’ve had this on my to-do list for a long time, and have finally gotten around to looking into it. We’ve largely migrated off Public Folders (thank goodness), but we still have thousands of empty folders sitting there.

I’d originally thought about writing some sort of crazy tree-traversal script to delete empty folders, but as it turns out, this alternative does the job fine. It allows you to delete batches of empty folders at a time. You could schedule the script to run once every hour or so.

The script does the following:

  1. Gets the first 500 Public Folders, narrows that down to those that don’t have subfolders
  2. Filters those down again, to exclude folders that contain items
  3. Deletes the folders, logging the folder name to c:temppf_delete.log
$folders = Get-PublicFolder -Identity "folder name" -Recurse -ResultSize 500 | Where-Object {$_.HasSubFolders -eq $false}
$emptyFolders = $folders | Where-Object -FilterScript {@($_ | Get-PublicFolderItemStatistics).Count -eq 0}
$emptyFolders | ForEach-Object {$_.Identity.ToString() | Out-File c:temppf_delete.log -append; Remove-PublicFolder -Identity $_.Identity -Confirm:$False}

I originally used Get-PublicFolderStatistics to determine the item count within the folder, but that was really slow. I ended up using @(Get-PublicFolderItemStatistics).Count instead, as that was much faster. The latter takes 1 second per folder, as opposed to 21 seconds per folder for the former.

An alternate method would be to loop over the above routine several times, each time retrieving less items. This will clean up nested folders from the top of the tree a little faster:

(1..5) | % {
  $folders = Get-PublicFolder -Identity "folder name" -Recurse -ResultSize 100 | Where-Object {$_.HasSubFolders -eq $false}
  $emptyFolders = $folders | Where-Object -FilterScript {@($_ | Get-PublicFolderItemStatistics).Count -eq 0}
  $emptyFolders | ForEach-Object {$_.Identity.ToString() | Out-File c:temppf_delete.log -append; Remove-PublicFolder -Identity $_.Identity -Confirm:$False}
}

Obviously, you need to test this thoroughly before running it in a production environment. Run at your own risk.

11 thoughts on “Remove Empty Public Folders in 3 Lines of PowerShell

  1. Hi,. Thanks for the script, I tried running this script on my exchange 2007 environment and it tells me Get-PublicFolderItemStatistics is not a valid command, SO I tried Get-PublicFolderstatistics instead but it gives me an error that I can’t call a null-valued expression at this step {$_.Identity.ToString()

    Any Insight how to get this to work on an exchange 2007 server ?

    Thanks

    Like

    • Hi Paulo,

      It appears that Get-PublicFolderItemStatistics is only supported on Exchange 2010 and newer.

      I don’t have access to an Exchange 2007 install, but you could try replacing the second line with:
      $emptyFolders = $folders | Where-Object -FilterScript {@($_ | Get-PublicFolderStatistics).ItemCount -eq 0}

      Regards,
      Daniel

      Like

      • Hmmm, I get the exact same error at line 3 char 53 |cannot call a method on a null-valued expression

        and then a |cannot bind argument to parameter ‘identity’ because it is null

        Like

      • I had this work fine in a number of my subdirectories (Exchange 2010) but now I’m actually getting this same set of errors in a different one. Odd.

        Like

      • I mentioned this above – I had this work fine in a number of my subdirectories but now I’m actually getting the same set of errors listed by another commenter in a different one.

        “|cannot call a method on a null-valued expression”
        and
        “|cannot bind argument to parameter ‘identity’ because it is null”

        I’m trying out a couple of hunches and will report back if I’m able to bludgeon it into working.

        Like

      • Well, when you’re using it to tidy existing folders, you’ll null out because you don’t have 500 bottom-level folders that are empty after a run or two. So that was silly.

        I’m now getting errors regarding accessing the log file as things are deleted, but I can handle that. Thanks again!

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s