PowerShell: Script to import calendar items into Exchange mailbox from CSV file


A recent case brought up the question of whether it is possible to import calendar items into a mailbox from a text file using PowerShell.  While there aren't any cmdlets built-in that support this, it is certainly possible to implement this using PowerShell and the EWS Managed API.  Sample script is attached, and the syntax is:

 

Import-CalendarCSV -CSVFileName <string> -EmailAddress <string> [-Username <string> -Password <string> [-Domain <string>]] [-Impersonate <bool>] [-EwsUrl <string>] [-EWSManagedApiPath <string>]

Parameters:

(Required)

-CSVFileName
Filename of the CSV file to import appointments for this user from.

-EmailAddress
Mailbox SMTP email address

(Optional)

-Username
Username for the account being used to connect to EWS (if not specified, current user is assumed)

-Password
Password for the specified user (required if username specified)

-Domain
If specified, used for authentication (not required even if username specified)

-Impersonate
Set to $true to use impersonation.

-EwsUrl
Forces a particular EWS URl (otherwise autodiscover is used, which is recommended)

-EWSManagedApiDLLFilePath
Full and path to the DLL for EWS Managed API (if not specified, default path for v1.1 is used)

Example

To process the CSV file c:\calendar.csv and import all entries into the mailbox of user@domain.com, you would use the following command:

Import-CalendarCSV -CSVFileName c:\calendar.csv -EmailAddress user@domain.com

The CSV needs to be in the format Subject, Start Date, Start Time, End Date, End Time.

Other fields can be added, and if this is the case you will need to add a header row to the CSV file.  The headers need to include the five required fields (Subject, Start Date, Start Time, End Date, End Time) and any other fields you like.  You should be able to use any of the fields listed here as additional fields, so long as they are simple (i.e. just contain a single value); this means that you can't specify Attachments, for example.

Import-CalendarCSV.zip

Comments (5)

  1. Ben says:

    Hi,

    This script would be very handy.  When we run it, we get the following error:

    "Exception calling "AutodiscoverUrl" with "1" argument(s): "Autodiscover blocked a potentially insecure redirection to autodiscover-s.outlook.com/…/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload."

    If I add the EWS URL as a switch, using outlook.office365.com/…/exchange.asmx I get the following error:

    "Exception calling "Bind" with "2" argument(s): "The request failed. The remote server returned an error: (401)

    Unauthorized.""

    I have tested using EWS Editor, and Auto DIscover works.  Can you point me in the right direction?

    Thanks

    Ben

  2. bluefiore says:

    I'm getting this error 5 times, I gues for every value in csv test file.

    Unable to index into an object of type System.Management.Automation.PSObject.

    At C:tmpImport-CalendarCSV.ps1:72 char:16

    +     if (!$CSVFile[ <<<< 0].$Key)

       + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException

       + FullyQualifiedErrorId : CannotIndex

    When i do Import-Csv it reads values just fine.

    At the end of script I get "Created" & subject, but no entry in calendar. Any suggestions? Thanks in advanced.

  3. Georg says:

    Hi,

    could this method work with a public folder as target?

    I tried but i get an error with autodiscover,

    Exception with autodiscoverUrl

    CategoryInfo: not specified

    FQErrorID: DotnetMethodException

  4. Hussain says:

    Hi,

    This is an excellent idea to implement it to group of students. Will it be possible to target member Mailbox of  Distribution Group?

    What about looping through multiple entries in a CSV file for 400 students?

    Thanks,

  5. ab says:

    Hi,

    Is there anyway to import it to a non-default calendar ?

    thank you

Skip to main content