Creating folder using EWS Managed API 1.1 from PowerShell

A customer of mine wanted to create a folder under Inbox for thousands of mailboxes. He also wanted to use PowerShell to do it. What better way that using Exchange Web Service(Managed API)  with Impersonation to do the job.

The list of the users are available in a text file. The First row denotes the Field Names.The file is named UserAccounts.txt. Format of the text file is as below:


Create a .PS1 file as below:

# The script requires the EWS managed API, which can be downloaded here:
# This also requires PowerShell 2.0
# Make sure the Import-Module command below matches the DLL location of the API.
# This path must match the install location of the EWS managed API. Change it if needed.

[string]$info = "White" # Color for informational messages
[string]$warning = "Yellow" # Color for warning messages
[string]$error = "Red" # Color for error messages
[string]$LogFile = "C:\Temp\Log.txt" # Path of the Log File

function CreateFolder($MailboxName)
Write-host "Creating Folder for Mailbox Name:" $MailboxName -foregroundcolor $info
Add-Content $LogFile ("Creating Folder for Mailbox Name:" + $MailboxName)

#Change the user to Impersonate
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName);

#Create the folder object

$oFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
$oFolder.DisplayName = $FolderName

#Call Save to actually create the folder

Write-host "Folder Created for " $MailboxName -foregroundcolor $warning
Add-Content $LogFile ("Folder Created for " + $MailboxName)

$service.ImpersonatedUserId = $null

#Change the name of the folder
$FolderName = "My Folder"

Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)

# Set the Credentials
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("serviceAccount","Password","Domain")

# Change the URL to point to your cas server
$service.Url= new-object Uri(http://CAS-Server/EWS/Exchange.asmx)

# Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
$UseAutoDiscover = $false

#Read data from the UserAccounts.txt
import-csv UserAccounts.txt | foreach-object {
$WindowsEmailAddress = $_.WindowsEmailAddress.ToString()

if ($UseAutoDiscover -eq $true) {
Write-host "Autodiscovering.." -foregroundcolor $info
$UseAutoDiscover = $false
Write-host "Autodiscovering Done!" -foregroundcolor $info
Write-host "EWS URL set to :" $service.Url -foregroundcolor $info

#To catch the Exceptions generated
trap [System.Exception]
Write-host ("Error: " + $_.Exception.Message) -foregroundcolor $error;
Add-Content $LogFile ("Error: " + $_.Exception.Message);

To Run the script:
1)Open the Exchange Management Shell
2)Navigate to the location where you have stored the Data and the script file.
3)Type in .\ScriptName.PS1 and hit enter to execute the script


Comments (4)

  1. Hi

    Will this work for Exchange 2007 sp2 as well.

    Can you suggest any changes if required.

  2. Akashb says:

    Yes, this should work on Exchange 2007 SP 2 as well. The only change would be to change the ExchangeVersion to Exchange2007_SP2

    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP2)

  3. gr8gordo says:

    If we want to create a folder in the archive mailbox for the email address do we only change the


    "$oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot)"  ?

  4. Akashb says:

    Yes that should do it. It will create a folder at root(Same level as Inbox in the MsgFolderRoot).

Skip to main content