EWS Script: Create a Folder in a Mailbox

Scenario:  You want to use EWS to create a folder in a mailbox.

Script:

###Load the EWS Assembly
    Import-Module -Name "C:Program FilesMicrosoftExchange ServerV15BinMicrosoft.Exchange.WebServices.dll"

    $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.Exchangeversion]::exchange2013)

    $service.Url = new-object System.Uri("https://mail.domain.com/EWS/Exchange.asmx")

###Pick the Mailbox
$mailboxname = "steve@mail.com"

#Creates a Folder 
    $NewFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
    $NewFolder.DisplayName = "Archive123"


#Save the folder under the Inbox
    $folderidInbox = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
    $NewFolder.Save($folderidInbox)

    #--OR--#

    #Save that folder on the Root
    $RootFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$mailboxname)   
    $NewFolder.Save($RootFolderID)
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: