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:
WindowsEmailAddressakasha@contoso.comakashb@contoso.comakashc@contoso.com
# The script requires the EWS managed API, which can be downloaded here:# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1# 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 Filefunction 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 $oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 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.txtimport-csv UserAccounts.txt | foreach-object { $WindowsEmailAddress = $_.WindowsEmailAddress.ToString() if ($UseAutoDiscover -eq $true) { Write-host "Autodiscovering.." -foregroundcolor $info $UseAutoDiscover = $false $service.AutodiscoverUrl($WindowsEmailAddress) 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); continue; } CreateFolder($WindowsEmailAddress)}
# The script requires the EWS managed API, which can be downloaded here:# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1# 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 $oFolder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 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.txtimport-csv UserAccounts.txt | foreach-object { $WindowsEmailAddress = $_.WindowsEmailAddress.ToString() if ($UseAutoDiscover -eq $true) { Write-host "Autodiscovering.." -foregroundcolor $info $UseAutoDiscover = $false $service.AutodiscoverUrl($WindowsEmailAddress) 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); continue; } CreateFolder($WindowsEmailAddress)}
Enjoy!