{"id":13841,"date":"2011-05-24T00:01:00","date_gmt":"2011-05-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/05\/24\/use-powershell-to-export-outlook-calendar-information\/"},"modified":"2011-05-24T00:01:00","modified_gmt":"2011-05-24T00:01:00","slug":"use-powershell-to-export-outlook-calendar-information","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-export-outlook-calendar-information\/","title":{"rendered":"Use PowerShell to Export Outlook Calendar Information"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to export Microsoft Outlook calendar information.<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>&nbsp; Hey, Scripting Guy! I was reading your Hey Scripting Guy! blogs that were talking about the 2011 Scripting Games. I was unable to compete this year, but that did not prevent me from watching the games. I bet you did not know that Windows PowerShell scripting was a spectator sport did you? Anyway, during the wrap-up blogs, you seemed to harp on things like &ldquo;functions always need to return an object&rdquo; and &ldquo;avoid using <b>Write-Host<\/b>.&rdquo; Now, I know that you did not write all of the blogs, but because the information appeared in the Hey Scripting Guy! blog, I am assuming that you at least read some of these things. Here is my question: If these are the sort of things that I am supposed to do in my scripts, why don&rsquo;t you do them in your blogs? Put another way, <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/01\/29\/how-can-i-display-my-office-outlook-appointments-without-starting-office-outlook.aspx\">here is a script you wrote to work with Microsoft Outlook calendar appointments<\/a>, and it probably would have received a 2 in the 2011 Scripting Games. <\/p>\n<p>&mdash;SH<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello SH, <\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. OK, SH you got me. I do not always floss my teeth every day, I do not always exercise an hour every day, I occasionally drink coffee, and have even been known to eat a pizza. I have tons of commitments, many of which are actually contradictory; I am swamped with meetings, email, and conference calls; not to mention that it takes a very long time to write each blog. When I decide to do special events like the 2011 Scripting Games, things get even more hectic. This event took nearly six months of preparation, and it involved dozens of hours of meetings with legal, hundreds of emails to sponsors, judges, and guest commentators. I had numerous conversations, in person and via email, with Joel Bennett (aka JayKul of PoshCode), and I even setup a tracking project on CodePlex. <\/p>\n<p>While all this was going on, I still had to publish a blog seven days a week. Oh yeah, there was the little matter of making up 20 events and writing 20 articles to describe those events and writing sample solutions for each of those events&mdash;that took several months to do as well. <\/p>\n<p>In short, I am a lot like you. I have to make choices as to where I spend my time. I could reduce the number of blogs that I write, and maybe only publish one a week. I could then spend the time to implement robust error handling, return objects from functions, implement comment-based Help, and the like. Or I can write and publish blogs seven days a week and write code that illustrates how to perform certain actions. Along the way, I can also illustrate how to perform robust error handling, return objects from functions, implement comment-based Help and the like&mdash;this is the approach I have chosen. In reality, all code does not need to be highly reusable, robust, and heavily commented. <\/p>\n<p>Oh, by the way, a 2 in the 2011 Scripting Games is a good score. It means that the script works and meets the requirements&mdash;dude, there is nothing wrong with that! On the other hand, when one gets a 2 out of 5, it means that there is room for improvement. <\/p>\n<p>The script I wrote last year, was designed to accomplish a single thing&mdash;it returned a week&rsquo;s worth of meetings. By converting the script to a function that returns an object, you now have a lot of flexibility in that you can return any different combination of date ranges, and you can sort or group your meetings in any way that you wish. <\/p>\n<p>In fact, you can use the power of Windows PowerShell to manipulate your Outlook appointments however you see fit. If you find a particular way of working with the data that you like, you can copy the function to your new script and use it there. You can store the function into its own Windows PowerShell script, in a particular location and dot source the script that contains the function into you new &ldquo;master&rdquo; script that creates the view you like. <\/p>\n<p>In addition, you can put the function into a module and use it from there. By making the code reusable, it opens up myriad possibilities for building upon the work that I did in writing the code to retrieve Outlook appointments. In fact, you do not need to touch the <b>Get-OutlookCalendar<\/b> function again. All you need to do is decide how you want to use the information. <\/p>\n<p>The complete <b>Get-OutlookCalendar<\/b> function is shown here. I have also copied it to the <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/a20bf555-1e51-4038-9739-36d153deaffa\">Scripting Guys Script Repository<\/a>. <\/p>\n<p><b>Get-OutlookCalendar function<\/b><\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Function Get-OutlookCalendar<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp; <\/span><\/span><span>&lt;#<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Synopsis<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>This function returns appointment items from default Outlook profile<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Description<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>This function returns appointment items from default Outlook profile. It<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>uses the Outlook interop assembly to use the olFolderCalendar enumeration.<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>It creates a custom object consisting of Subject, Start, Duration, Location<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>for each appointment item. <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Example<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Get-OutlookCalendar | <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>where-object { $_.start -gt [datetime]&#8221;5\/10\/2011&#8243; -AND $_.start -lt `<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[datetime]&#8221;5\/17\/2011&#8243; } | sort-object Duration<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Displays subject, start, duration and location for all appointments that<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;<\/span>occur between 5\/10\/11 and 5\/17\/11 and sorts by duration of the appointment.<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>The sort is shortest appointment on top. <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Notes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>NAME:<span>&nbsp; <\/span>Get-OutlookCalendar<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>AUTHOR: ed wilson, msft<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>LASTEDIT: 05\/10\/2011 08:36:42<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>KEYWORDS: Microsoft Outlook, Office<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>HSG: HSG-05-24-2011<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Link<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Http:\/\/www.ScriptingGuys.com\/blog<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>#Requires -Version 2.0<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>#&gt;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>Add-type -assembly &#8220;Microsoft.Office.Interop.Outlook&#8221; | out-null<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$olFolders = &#8220;Microsoft.Office.Interop.Outlook.OlDefaultFolders&#8221; -as [type] <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$outlook = new-object -comobject outlook.application<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$namespace = $outlook.GetNameSpace(&#8220;MAPI&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$folder.items |<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>Select-Object -Property Subject, Start, Duration, Location<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">} #end function Get-OutlookCalendar<\/span><\/span><\/span><\/p><\/blockquote>\n<p>To use the <b>Get-OutlookCalendar<\/b> function, you need to first load it into memory. The easiest way to do this is to open the script that contains the <b>Get-OutlookCalendar<\/b> function and execute the code. Now, I say open the script that contains the <b>Get-OutlookCalendar <\/b>function, but the function could be contained in a module, in which case you would need to import the module by using the <b>Import-Module<\/b> cmdlet. After you have loaded the function, you can use &ldquo;normal&rdquo; Windows PowerShell cmdlets to manipulate the data. <\/p>\n<p>The largest portion of the <b>Get-OutlookCalendar<\/b> function is the comment-based Help. It was also the easiest to create. This is because I used my <b>Add-Help<\/b> function that I added to my Windows PowerShell ISE profile. Refer to the <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts.aspx\">Automatically Add Comment-Based Help to Your PowerShell Scripts<\/a> Weekend Scripter blog for the <b>Add-Help<\/b> function and its use. The complete Help portion of the function is shown here. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&lt;#<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Synopsis<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>This function returns appointment items from default Outlook profile<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Description<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>This function returns appointment items from default Outlook profile. It<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>uses the Outlook interop assembly to use the olFolderCalendar enumeration.<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>It creates a custom object consisting of Subject, Start, Duration, Location<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>for each appointment item. <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Example<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Get-OutlookCalendar | <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>where-object { $_.start -gt [datetime]&#8221;5\/10\/2011&#8243; -AND $_.start -lt `<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[datetime]&#8221;5\/17\/2011&#8243; } | sort-object Duration<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Displays subject, start, duration and location for all appointments that<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>occur between 5\/10\/11 and 5\/17\/11 and sorts by duration of the appointment.<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>The sort is shortest appointment on top. <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Notes<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>NAME:<span>&nbsp; <\/span>Get-OutlookCalendar<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>AUTHOR: ed wilson, msft<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>LASTEDIT: 05\/10\/2011 08:36:42<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>KEYWORDS: Microsoft Outlook, Office<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>HSG: HSG-05-24-2011<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp; <\/span><\/span><span>.Link<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Http:\/\/www.ScriptingGuys.com\/blog<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>#Requires -Version 2.0<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>#&gt;<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Following the Help portion of the function, I use the <b>Add-Type<\/b> cmdlet to add the Outlook interop assembly. I then create the <b>OlDefaultFolders <\/b>type enumeration. This allows me to reference the calendar folder by using the enumeration <b>OlFolderCalendar<\/b> instead of using the numerical equivalent. I prefer to avoid the direct use of numbers without explanation. In the VBScript days, I would create a variable called <b>OlFolderCalendar<\/b> and assign the value <b>9<\/b> to it. <\/p>\n<p>By the way, finding the numerical value of <b>enum<\/b> is often an exercise in futility. Whereas the <b>enum<\/b> names themselves are always documented on MSDN, the numerical value is often not documented. This is one reason I wrote my <b>Get-EnumAndValues<\/b> function that is discussed in the Weekend Scripter blog, <a target=\"_blank\" href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-enumerations-and-values\/\">Enumerations and Values<\/a>. In fact, the use of enumerations is a very important subject, and it is something about which I have written <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/enum\/\">several blogs<\/a>. You should check them out.<\/p>\n<p>After I create the calendar folder enumeration, I connect to the MAPI namespace and retrieve the calendar folder. This is all pretty standard stuff when working with Microsoft Outlook. These five lines of code are shown here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Add-type -assembly &#8220;Microsoft.Office.Interop.Outlook&#8221; | out-null<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$olFolders = &#8220;Microsoft.Office.Interop.Outlook.OlDefaultFolders&#8221; -as [type] <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$outlook = new-object -comobject outlook.application<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$namespace = $outlook.GetNameSpace(&#8220;MAPI&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>$folder = $namespace.getDefaultFolder($olFolders::olFolderCalendar)<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The really cool thing is that the next two lines of code are the ones that convert the Windows PowerShell script from a single purpose script into a highly reusable function. What is really, really cool, is that it takes less code &ldquo;to do the right thing&rdquo; than it did to create a single purpose script. Here are the two lines of code:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$folder.items |<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;<\/span><\/span><span>Select-Object -Property Subject, Start, Duration, Location<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>&ldquo;But wait. I thought you said that functions should return objects,&rdquo; you may ask. <\/p>\n<p>&ldquo;Yes that is correct,&rdquo; I answer. <\/p>\n<p>The really, really, really cool thing is that <b>Select-Object<\/b> returns a custom object. This is the easiest way to create an object. <\/p>\n<p>Suppose that you decide you want to see a grouping of all the meetings for a particular week, arranged by the duration of the meetings. You want to see the shortest meetings on top and the longest meetings on the bottom of the list. You can use the following code to accomplish this task.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-OutlookCalendar | <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>where-object { $_.start -gt [datetime]&#8221;5\/10\/2011&#8243; -AND $_.start -lt `<\/span><\/span><\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[datetime]&#8221;5\/17\/2011&#8243; } | sort-object Duration<\/span><\/span><\/span><\/span><\/p><\/blockquote>\n<p>Perhaps, you are interested in where most of your appointments are occurring. This time, you do not want to limit the results to a particular time span. You want the most frequent locations to appear on the top of the list. To do this, you could use the following command.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">Get-OutlookCalendar | Group-Object -Property Location | <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Sort-Object count &ndash;Descending<\/span><\/span><\/span><\/span><span><\/span><\/p>\n<\/blockquote>\n<p>By converting the old Windows PowerShell single-purpose script into a function, not only is the code simpler and easier to understand, it is also more flexible. An example of using the <b>Get-OutlookCalendar<\/b> function appears in the following image. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7043.HSG-5-24-11-1_07419C74.jpg\"><img decoding=\"async\" height=\"362\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2768.HSG-5-24-11-1_thumb_7FB62D06.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>The complete <b>Get-OutlookCalendar<\/b> function is available in the <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/a20bf555-1e51-4038-9739-36d153deaffa\">Scripting Guys Script repository<\/a>. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to export Microsoft Outlook calendar information. &nbsp; Hey, Scripting Guy! I was reading your Hey Scripting Guy! blogs that were talking about the 2011 Scripting Games. I was unable to compete this year, but that did not prevent me from watching the games. I bet you did not [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,4,45,77],"class_list":["post-13841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to export Microsoft Outlook calendar information. &nbsp; Hey, Scripting Guy! I was reading your Hey Scripting Guy! blogs that were talking about the 2011 Scripting Games. I was unable to compete this year, but that did not prevent me from watching the games. I bet you did not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=13841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}