I've had a few questions about this in the past week so I thought I would put together a quick sample to show how you can create a new Folder is EWS and set the FolderClass so it appears as a Calendar, Contacts, Tasks, Notes or whatever other folder you want. This is a function so you can just cut and paste it into your own script to use. The full script looks like the following which has samples of using the function at the bottom.
- ## Get the Mailbox to Access from the 1st commandline argument
- $MailboxName = $args[0]
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $psCred = Get-Credhttp://www.blogger.com/blogger.g?blogID=7123450#editor/target=post;postID=2287688273890342518ential
- $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- ## Code From http://poshcode.org/624
- ## Create a compilation environment
- $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
- $Compiler=$Provider.CreateCompiler()
- $Params=New-Object System.CodeDom.Compiler.CompilerParameters
- $Params.GenerateExecutable=$False
- $Params.GenerateInMemory=$True
- $Params.IncludeDebugInformation=$False
- $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
- $TASource=@'
- namespace Local.ToolkitExtensions.Net.CertificatePolicy{
- public class TrustAll : System.Net.ICertificatePolicy {
- public TrustAll() {
- }
- public bool CheckValidationResult(System.Net.ServicePoint sp,
- System.Security.Cryptography.X509Certificates.X509Certificate cert,
- System.Net.WebRequest req, int problem) {
- return true;
- }
- }
- }
- '@
- $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
- $TAAssembly=$TAResults.CompiledAssembly
- ## We now create an instance of the TrustAll and attach it to the ServicePointManager
- $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
- [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
- ## end code from http://poshcode.org/624
- ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #CAS URL Option 2 Hardcoded
- #$uri=[system.URI] "https://casservername/ews/exchange.asmx"
- #$service.Url = $uri
- ## Optional section for Exchange Impersonation
- #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
- function Create-Folder{
- param (
- $ParentFolderID = "$( throw 'Enter Id of Parent Folder' )",
- $FolderName = "$( throw 'Enter Folder Name' )",
- $FolderClass = "$( throw 'Enter Folder Class IPF.Note,IPF.Appointment,IPF.Contact,IPF.Task' )"
- )
- process{
- $NewFolder = new-object Microsoft.Exchange.WebServices.Data.Folder($service)
- $NewFolder.DisplayName = $FolderName
- $NewFolder.FolderClass = $FolderClass
- try{
- $NewFolder.Save($ParentFolderID)
- Write-Host $FolderName + " Folder Created "
- }
- catch{
- "Error : " + $Error[0]
- }
- }
- }
- #Example use creating a Calendar subFolder
- $ParentId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
- Create-Folder -ParentFolderID $ParentId -FolderName "New Calendar Folder Name" -FolderClass "IPF.Appointment"
- #Example use creating a Contacts subFolder
- $ParentId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName)
- Create-Folder -ParentFolderID $ParentId -FolderName "New Contact Folder Name" -FolderClass "IPF.Contact"
- #Example use creating a Tasks subFolder
- $ParentId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Tasks,$MailboxName)
- Create-Folder -ParentFolderID $ParentId -FolderName "New Tasks Folder Name" -FolderClass "IPF.Task"
- #Example use creating a Notes subFolder
- $ParentId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Notes,$MailboxName)
- Create-Folder -ParentFolderID $ParentId -FolderName "New Notes Folder Name" -FolderClass "IPF.StickyNote"