I tweaked the PowerShell at Print Classic Outlook Folder Names to print a list of folder names that are used for email messages and if the folder is empty, deletes it. If you run the script once, it moves the empty folders to the deleted items folder, running the script a second time deletes the folders from the Deleted Items. (Or right click on the Deleted Items folder and choose Empty Folder to empty the Deleted items folder.)
RSS folders are considered Mail folders, and will be processed by the script and deleted if empty.
Default folders that are empty, such as the Outbox, will not be deleted.
Note: in my tests, it did not delete all the empty folders on the first pass.
I recommend doing a test run by either deleting the $Folder.delete() line or adding a # in front to comment it out: #$Folder.delete(). Folders are moved to Deleted items folder; if any are moved that you don't want deleted, move the folder back and copy a message into it so it is not deleted if you run the script again.
The script checks both for an empty folder (no messages) and that the folder does not contain subfolders. It only checks mail folders, not calendar, contacts, or tasks. If you use RSS feeds, the folders are considered email folders, and they will be deleted if empty.
The script will write the folder names to the PowerShell window before deleting. An error will display if the folder is a default folder and cannot be deleted.
clear $olApp = New-Object -comobject Outlook.Application $namespace = $olApp.GetNameSpace("MAPI") #pick data store or enter data file display name $datafile = $namespace.PickFolder().store $mailbox = $datafile.GetRootFolder() #used in text file name $Date = Get-Date -Format "yyyy-MM-dd.h-m-s" # Hidden folder property $PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B" Function Listfolders { param($Folders, $Indent) ForEach ($Folder in $Folders) { #only list mail folders (includes RSS feeds) if ($folder.DefaultMessageClass -eq "IPM.Note") { # Check to see if folder is visible $oPA = $Folder.PropertyAccessor $value = $oPA.GetProperty($PropName) if ($value -ne $true) { #mailbox name and full folder path indented #write to text file echo $Indent$($Folder.FullFolderPath)" ("$($Folder.Items.Count)")" >> ~\Documents\Foldernames_$Date.txt # if the folder is empty and does not have subfolders, delete folder if ($Folder.Items.Count -eq 0 -AND $Folder.folders.count -eq 0) { write-host $Folder.name $Folder.delete() } } } Listfolders $Folder.Folders $Indent" " } } ListFolders $mailbox. Folders ""
Using PowerShell Scripts
To use PowerShell scripts with Outlook, start typing PowerShell on the start menu and open Windows PowerShell when it comes up. Windows PowerShell ISE has a script pane at the top, which is useful if you want to edit the script.
Paste the entire script in the PowerShell window and press Enter or the Run button if using PowerShell ISE. If you don't have the white window at the top of the ISE window, click the V icon on the right to expand it.
Note: PowerShell scripts will not work with new Outlook or Outlook on the web.
Saving PowerShell Scripts
If you want to save the script as a .ps1 file, paste it into Notepad and save it with the extension .ps1. To open it in the PowerShell ISE, type PowerShell on the start menu and click on Windows PowerShell ISE when the PowerShell app is found. Paste the script in the editing window.
To use it, you need to allow local scripts by running this command:
Set-ExecutionPolicy RemoteSigned
To run your saved .ps1 file, right-click on the script and choose Run with PowerShell.