I have a PowerShell sample to delete folders from Outlook and user asked how to use PowerShell to create folders. The PowerShell samples on this page create subfolders in the Inbox, or, by changing one line, add the folders to the root of the mailbox, at the same level as the Inbox.
The first PowerShell has the folder name in the Folders.Add line. If you need more than one folder, repeat the line. However, it is generally much easier to create a text file with one folder name per line than to copy and edit each line. The second set of PowerShell scripts use a text file for the folder names.
To add the folder at the same level as the Inbox, change this line:
$Folder.Folders.Add("Scripts")
to
$Folder.Parent.folders.Add("Scripts")
$olApp = new-object -comobject outlook.application $namespace = $olApp.GetNamespace("MAPI") # Get Inbox $Folder = $namespace.GetDefaultFolder(6) # Repeat this line as needed, changing the folder name in each line $Folder.Folders.Add("Scripts") # End Inbox $olApp.Quit | Out-Null [GC]::Collect()
Create subfolders using a list of filenames
This PowerShell script reads the folder names from a text file. Use this is you need to frequently create new folders as the text file is generally easier to update.
To add the folders at the same level as the Inbox, change this line:
$Folder.Folders.Add($subfolder)
to
$Folder.Parent.folders.Add($subfolder)
Don't forget to change the path to the text file!
Enter your folder names in a text file, one name per line.
$olApp = new-object -comobject outlook.application $namespace = $olApp.GetNamespace("MAPI") $Folder = $namespace.GetDefaultFolder(6) $addnewFolders= New-Object System.IO.StreamReader("D:\Documents\newfolders.txt") while (($subfolder =$addnewFolders.ReadLine()) -ne $null) { $Folder.Folders.Add($subfolder) } $addnewFolders.Dispose() $olApp.Quit | Out-Null [GC]::Collect()
Like the script above, this PowerShell script reads the folder names from a text file. It just uses a different method to read the text file.
$olApp = new-object -comobject outlook.application $namespace = $olApp.GetNamespace("MAPI") $Folder = $namespace.GetDefaultFolder(6) foreach($subfolder in Get-Content "D:\Documents\newfolders.txt") { $Folder.Folders.Add($subfolder) } $olApp.Quit | Out-Null [GC]::Collect()
Add folders to a secondary mailbox in your profile
The scripts above work with the default data file; to add the folders to a secondary data file in your profile, you need add another line to identify the data file. You'll also need ot refer to the parent folder by name.
$olApp = new-object -comobject outlook.application $namespace = $olApp.GetNamespace("MAPI") $Folders = $namespace.Folders.Item("alias@domain.com") #parent folder $Folder = $Folders.Folders.Item("Inbox") foreach($subfolder in Get-Content "D:\Documents\newfolders.txt") { $Folder.folders.Add($subfolder) } $olApp.Quit | Out-Null [GC]::Collect()
Delete the folders using PowerShell
If you need to delete the folders you added, this PowerShell will read the text file and delete the folders. This script moves the folders to the deleted items folder. To delete them from the Deleted Items folder, change 6 to a 3 in this line:
$Folder = $namespace.GetDefaultFolder(6)
$olApp = new-object -comobject outlook.application $namespace = $olApp.GetNamespace("MAPI") # delete subfolders from Inbox folder $Folder = $namespace.GetDefaultFolder(6) foreach($subfolder in Get-Content "D:\Documents\newfolders.txt") { $Folder.Folders.item($subfolder).Delete() } $olApp.Quit | Out-Null [GC]::Collect()
Using PowerShell Scripts
To open the PowerShell IDE, 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.
Note: This PowerShell script will not work with the Windows Store version of Office. You'll need to use the macro version if you have the Windows store version of Office installed.
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 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.
More Information
A VBA macro that creates folders from a CSV list of filenames is here: Create Outlook Folders from a List of Folder Names
To delete folders, see Delete folders using a macro or PowerShell
Unfortunately I have not figured out how to create a sub-sub folder?
Just using "\" or "/" in the name doesn't work.
Maybe this could be extended here?
You need to "walk" the folders.
$Folder = $namespace.GetDefaultFolder(6).Folders("parent folder name")
# Repeat this line as needed, changing the folder name in each line
$Folder.Folders.Add("Scripts")
That would create subfolder at Inbox\parent folder name\scripts
This is awesome, thank you.
Thank you!