Public Folders

Public Folders are the bane of every Exchange administrator. They’re quite nice to have, from the point of view of a user, at least, but keeping control of them can be a serious pain. It would be so much easier if these things could be just picked up and lifted straight from Exchange and dumped into SharePoint, but until we can do that, we have to manage them.

Some times, important details can be held in the Description field on the Public Folder item, however you can’t get this information using the built-in Get-PublicFolder or Get-PublicFolderStatistics using the standard Exchange Management Shell. So how to get this information without having to check each folder manually?

Well, if you have Visual Studio at your disposal, create yourself a new C# Console Application project, add a .Net reference to Microsoft.Office.Interop.Outlook – this will require that Outlook is installed on the machine you’re using.

When done, you can use the code below to get your information. This starts at the root of the public folder tree and enumerates through all accessible public folders, obtaining key information about them, before writing out to a semicolon-delimited text file. This requires that the user running it has a MAPI profile and access to the public folder tree. Any folders you don’t have permission to access are skipped and not included in the enumerated results.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Public_Folder_Info
{
    class Program
    {
        public List<PublicFolder> AllFolders = new List<PublicFolder>();
        private static void Main(string[] args)
        {
            Program p = new Program();
            Console.Clear();
            Console.Title = "Outlook Public Folder Scanner";
            p.EnumRootFolder();
            p.OutputFile();
            Console.WriteLine("\nScan complete.");
            Console.ReadLine();
        }

        public void EnumRootFolder()
        {
            Outlook.Application app = new Outlook.Application();
            Outlook.Folder root = app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders) as Outlook.Folder;
            EnumerateFolders(root);
        }

        public void EnumerateFolders(Outlook.Folder fldr)
        {
            Outlook.Folders childfolders = fldr.Folders;
            if (childfolders.Count > 0)
            {
                foreach (Outlook.Folder childFolder in childfolders)
                {
                    Console.WriteLine("Scanning: " + childFolder.Name);
                    PublicFolder pf = new PublicFolder();
                    pf.FolderName = childFolder.Name;
                    pf.FolderPath = childFolder.FolderPath;
                    try
                    {
                        pf.ItemCount = childFolder.Items.Count;
                    }
                    catch
                    {
                        pf.ItemCount = -1;
                    }
                    pf.Owner = childFolder.Description;
                    AllFolders.Add(pf);
                    EnumerateFolders(childFolder);
                }
            }
        }

        public void OutputFile()
        {
            Console.WriteLine("\n" + AllFolders.Count.ToString() + " public folders scanned. Writing to file...");
            StreamWriter file = new StreamWriter(@"C:\Pubfldrs\PublicFolderOwners.csv"); // Change the path here to suit your needs.
            file.WriteLine("Folder Name;Folder Path;Item Count;Owner");
            foreach (PublicFolder pf in AllFolders)
            {
                file.WriteLine(pf.FolderName + ";" + pf.FolderPath + ";" + pf.ItemCount.ToString() + ";" + pf.Owner);
            }
        }

        public class PublicFolder
        {
            // Several properties are included in the Class which are not returned. Feel free to add the necessary code, or delete them from the class if required.
            public string FolderName { get; set; }
            public string FolderPath { get; set; }
            public DateTime Created { get; set; }
            public DateTime LastAccessed { get; set; }
            public DateTime LastModified { get; set; }
            public int ItemCount { get; set; }
            public int Size { get; set; }
            public bool MailEnabled { get; set; }
            public string PrimarySMTPAddress { get; set; }
            public string Owner { get; set; }
        }
    }
}

I hope this is useful for your all. If you have any comments, please feel free to let me know.

Share and Enjoy:
  • Digg
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Twitter
  • blogmarks
  • HackerNews
  • Tumblr
  • Posterous
  • email
Bookmark the permalink. Follow any comments here with the RSS feed for this post.
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.