I have a VBA macro that prints a list of classic Outlook folders to new message. It works great but requires the user to change macro security settings. The code on this page uses PowerShell and does not require changing macro security settings.
To use the PowerShell, you just need to paste the code into the PowerShell window. Change the file path if needed (it defaults to C:\Users\%username%\Documents.) If you want to change the sort order or include the full file paths, move the # sign in front of the lines to the other option. Then click the Run button or press F5 key.
This screenshot shows two printouts, both it just the folder names. The second one is sorted and includes the parent folder names.
This example shows the full file path.
Note: when you list the folders in a Microsoft Exchange account (including in Outlook.com accounts), it includes hidden folders. The code skips any folders marked with the hidden property but will trigger an error if the property does not exist on the folder. ("The property is unknown or cannot be found.") This does not affect the script. It is possible to disable error reporting in PowerShell, but it is not recommended.
Print a list of all folders in profile
Using the script, you can print the list of folder names in your profile (includes folders in all email accounts, in any public folders, and in PST files.)
This PowerShell creates a text file containing the list of folder names. You can include the full folder path or just the folder names and sort them alphabetically (or not). The message count is listed after the folder name.
clear $olApp = New-Object -comobject Outlook.Application $namespace = $olApp.GetNameSpace("MAPI") $Date = Get-Date -Format "yyyy-MM-dd.h-m-s" $PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B" Function Listfolders { param($Folders, $Indent) #Don't sort the folder named #ForEach ($Folder in $Folders) #Sort folder names ForEach ($Folder in $Folders | sort-object name) { # don't print hidden folders $oPA = $Folder.PropertyAccessor $value = $oPA.GetProperty($PropName) if ($value -ne $true) { #path and folder name # echo $Indent$($Folder.FullFolderPath)" ("$($Folder.Items.Count)")" >> ~\Documents\Foldernames_$Date.txt # folder name only echo $Indent$($Folder.Name)" ("$($Folder.Items.Count)")" >> ~\Documents\Foldernames_$Date.txt } Listfolders $Folder.Folders $Indent" " } } ListFolders $namespace.Folders ""
Print folders in a specific account or data file
If you have more than one email account or data file, this version of the PowerShell prints only the folders in the specific data file. You can either use the folder picker to pick the data file or enter the mailbox display name, as seen in Outlook's folder list.
Note: when using the folder picker, you can pick any folder in the data file. The script uses the root folder of the data file or mailbox in selection is in and prints all folders.
clear $olApp = New-Object -comobject Outlook.Application $namespace = $olApp.GetNameSpace("MAPI") #pick data store or enter data file display name #$datafile = $namespace.PickFolder().store $datafile = $namespace.stores | Where-Object { $_.displayname –eq ‘alias@domain.com’} $mailbox = $datafile.GetRootFolder() #used in text file name $Date = Get-Date -Format "yyyy-MM-dd.h-m-s" $PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B" Function Listfolders { param($Folders, $Indent) #Don't sort the folder named ForEach ($Folder in $Folders) #Sort folder names alphabetically #ForEach ($Folder in $Folders | sort-object name) { $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 #folder name only, indented #write to text file #echo $Indent$($Folder.Name)" ("$($Folder.Items.Count)")" >> ~\Documents\Foldernames_$Date.txt 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. (Click the icon on the right to show or hide the script window or the Window button in the toolbar.)
Paste the entire script in the PowerShell window and press Enter or the Run button. This screenshot is of PowerShell ISE.
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 IDE, type PowerShell on the start menu and click on Windows PowerShell IDE 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.