Skip to content

Instantly share code, notes, and snippets.

@azure365pro
Created May 13, 2020 17:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save azure365pro/783eff4a9d2d491bea4f924ee596b7c2 to your computer and use it in GitHub Desktop.
Save azure365pro/783eff4a9d2d491bea4f924ee596b7c2 to your computer and use it in GitHub Desktop.
Create a Custom root Folder for all the Mailboxes (Bulk) in Office365
[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]::msgfolderroot)
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 = "Customer Folder Name"
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchVer)
# Set the Credentials
$user = "sathesh@careexchange.onmicrosoft.com"
$pass = "bddre!fdsf(2**46"
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $user, $pass
# Change the URL to point to your cas server
$service.Url= new-object Uri("https://outlook.office365.com/ews/exchange.asmx")
# Set $UseAutoDiscover to $true if you want to use AutoDiscover else it will use the URL set above
$UseAutoDiscover = $true
$a = get-mailbox
$a | foreach-object {
$WindowsEmailAddress = $_.WindowsEmailAddress.ToString()
#if ($UseAutoDiscover -eq $true) {
# Write-host "Autodiscovering.." -foregroundcolor $info
#$UseAutoDiscover = $false
$TestUrlCallback = {
param ([string] $url)
if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false}
}
$service.AutodiscoverUrl($WindowsEmailAddress,$TestUrlCallback)
#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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment