{"id":51803,"date":"2009-12-14T00:01:00","date_gmt":"2009-12-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/14\/hey-scripting-guy-with-windows-powershell-can-i-get-a-list-of-e-mail-i-have-marked-for-follow-up\/"},"modified":"2009-12-14T00:01:00","modified_gmt":"2009-12-14T00:01:00","slug":"hey-scripting-guy-with-windows-powershell-can-i-get-a-list-of-e-mail-i-have-marked-for-follow-up","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-with-windows-powershell-can-i-get-a-list-of-e-mail-i-have-marked-for-follow-up\/","title":{"rendered":"Hey, Scripting Guy! With Windows PowerShell Can I Get a List of E-Mail I Have Marked for Follow-Up?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I don&rsquo;t know about you, but I get a ton of e-mail every day. Of course, I use inbox rules to filter my e-mail, but one thing that I seem to miss is e-mail that has been marked for follow-up. I would love to see a Windows PowerShell script that will troll my inbox and display a list of e-mail messages that have been marked for follow-up. Is this something that can be done?<\/p>\n<p class=\"MsoNormal\">&#8212; MS<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello MS, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. Today has been an awesome day. I downloaded Exchange 2010 from the Microsoft TechNet site (I got a free subscription earlier in the year when I attended Tech<span>&#8729;<\/span>Ed 2009 in Los Angeles) and burned it to DVD. I am now in the process of building an Exchange Server 2010 on my Windows Server 2008 R2 Hyper-V box. Over the Thanksgiving holidays, I built a couple of Windows XP virtual machines and installed Office 2007 on them. I need the machines because I am running Windows 7 with the latest build of Office 2010 beta, and I like to have something against which to test. <\/p>\n<p class=\"MsoNormal\">Because I used to do a lot of networking before I joined Microsoft, I really enjoying building machines, and setting up various test scenarios. Before virtual machines, my test lab consisted of 12 physical computers&mdash;it was fun, but expensive and noisy. With virtualization, I can have as many computers as I wish, as long as I have the disk space and software licenses (and between my MSDN subscription and my TechNet subscription, I can get quite a few software licenses). Of course, if I am not going to use a machine permanently, I do not activate it, which allows me even more flexibility. <\/p>\n<p class=\"MsoNormal\">Let&#8217;s whip up a new Windows&nbsp;PowerShell script to christen my&nbsp;bright and shiny new Exchange server on a Hyper-V box and a couple of new clients with fresh installs of Microsoft Outlook 2007. <\/p>\n<p class=\"MsoNormal\">MS, I totally sympathize with you regarding the follow-up flag. Items flagged for follow-up show up in my Outlook task list, but there is no easy way to filter them out. If your inbox runs to several thousand e-mails, like mine routinely does, scrolling up and down through the e-mail can be a bit problematic. <\/p>\n<p class=\"MsoNormal\">Just a second while I crank up the Alan Parsons Project (great music for writing Outlook scripts) on my Zune HD, roll up my sleeves, fire up the Windows PowerShell 2.0 ISE, and get started. <\/p>\n<p class=\"MsoNormal\">Okay, okay, if you only want the script, the complete ListFollowUpEmail.ps1 script is seen here.<\/p>\n<p class=\"CodeBlockScreenedHead\"><b>ListFollowUpEmail.ps1<\/p>\n<p><\/b><\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">#Requires -version 2.0<br>Add-Type -AssemblyName Microsoft.Office.Interop.Outlook<br>$olFolders = &#8220;Microsoft.Office.Interop.Outlook.OlDefaultFolders&#8221; -as [type]<br>$outlook = New-Object -ComObject outlook.application<br>$namespace<span>&nbsp; <\/span>= $Outlook.GetNameSpace(&#8220;mapi&#8221;)<br>$folder = $namespace.getDefaultFolder($olFolders::olFolderInbox)<br>$items = $folder.Items<br>&#8220;The following items are marked for followup:&#8221; <br>foreach($item in $items)<br>{<br><span>&nbsp;<\/span>if($item.flagStatus -eq 2)<br><span>&nbsp;&nbsp; <\/span>{ $item.subject } <br>}<\/font><\/p>\n<p class=\"MsoNormal\">The ListFollowUpEmail.ps1 script begins by loading the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms268893(VS.80).aspx\"><font face=\"Segoe\">Microsoft.Office.Interop.Outlook<\/font><\/a> .NET Framework assembly. You do not have to load the interop assembly, but if you do, it gives you direct access to the Microsoft Outlook enumerations such as the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.office.interop.outlook.oldefaultfolders.aspx\"><font face=\"Segoe\">OlDefaultFolders enumeration<\/font><\/a>. The <b>OlDefaultFolders<\/b> enumeration is used by the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219900.aspx\"><font face=\"Segoe\">GetDefaultFolder method<\/font><\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb208416.aspx\"><font face=\"Segoe\">NameSpace object<\/font><\/a>. To load the <b>Microsoft.Office.Interop.Outlook<\/b> interop assembly in Windows PowerShell 1.0, you could use the <b>LoadWithPartialName<\/b> static method from the <b>Reflections.Assembly<\/b> .NET Framework class. But the <b>LoadWithPartialName<\/b> static method is now obsolete (although it still works), and you should use the <b>Load<\/b> static method instead. The problem is that the <b>Load<\/b> static method from <b>Reflections.Assembly<\/b> requires the full name to the assembly. This is especially problematic because MSDN does not list the full name of the assembly. To determine the full name, you can use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.reflection.assembly.getassembly.aspx\"><font face=\"Segoe\">GetAssembly method<\/font><\/a> to retrieve the currently loaded assembly in which the specified class is defined. As seen here, we use the <b>$olFolders<\/b> enumeration class after we have loaded the Outlook interop assembly. <\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">PS C:&gt; [reflection.assembly]::GetAssembly($olFolders)<\/p>\n<p>GAC<span>&nbsp;&nbsp;&nbsp; <\/span>Version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Location<br>&#8212;<span>&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<br>True <span>&nbsp;&nbsp;<\/span>v2.0.50727<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>C:WindowsassemblyGAC_MSILMicrosoft.Office.Interop.Outlook14.0.0.0__71e9bce111e9429cMicro&#8230;<\/p>\n<p>PS C:&gt; [reflection.assembly]::GetAssembly($olFolders) | Format-List *<\/p>\n<p>CodeBase<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: file:\/\/\/C:\/Windows\/assembly\/GAC_MSIL\/Microsoft.Office.Interop.Outlook\/14.0.0.0__71e9bce111e9429c\/<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Microsoft.Office.Interop.Outlook.dll<br>EscapedCodeBase<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: file:\/\/\/C:\/Windows\/assembly\/GAC_MSIL\/Microsoft.Office.Interop.Outlook\/14.0.0.0__71e9bce111e9429c\/<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Microsoft.Office.Interop.Outlook.dll<br>FullName<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c<\/font><\/p>\n<p class=\"MsoNormal\">It would be silly to use an obsolete method to load an assembly and then use a different method to retrieve the full name of the assembly. However, that is what you would need to do in order to be able to use the <b>Load<\/b> method. Luckily, the <b>Add-Type<\/b> cmdlet was added to Windows PowerShell 2.0. This cmdlet will accept the partial assembly name, and therefore you do not need to worry about using an obsolete method. Because the <b>Add-Type<\/b> cmdlet only exists on Windows PowerShell 2.0, make sure to use the <b>#Requires &ndash;Version 2.0<\/b> tag to prevent errors with Windows PowerShell 1.0:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">#Requires -version 2.0<br>Add-Type -AssemblyName Microsoft.Office.Interop.Outlook<\/font><\/p>\n<p class=\"MsoNormal\">After you have loaded the Outlook interop assembly, you can load the <b>OlDefaultFolders<\/b> enumeration as a type so that you will be able to directly address the enumeration value. This is seen here where the <b>OlDefaultFolders<\/b> enumeration is stored in the <b>$olFolders<\/b> variable:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$olFolders = &#8220;Microsoft.Office.Interop.Outlook.OlDefaultFolders&#8221; -as [type]<\/font><\/p>\n<p class=\"MsoNormal\">Now it is time to create an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219938.aspx\"><font face=\"Segoe\">Outlook.Application COM object<\/font><\/a>. The application object is the basic object to use when working with automating Microsoft Outlook. It will be included in nearly all of the scripts you write that work with Microsoft Outlook. To create the <b>outlook.application<\/b> object, you use the <b>New-Object<\/b> cmdlet and specify the <b>&ndash;comobject<\/b> parameter. The return application object is stored in the <b>$outlook<\/b> variable. This is seen here:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$outlook = New-Object -ComObject outlook.application<\/font><\/p>\n<p class=\"MsoNormal\">Next, you need to create a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219955.aspx\"><font face=\"Segoe\">namespace object<\/font><\/a>. To do this, use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219904.aspx\"><font face=\"Segoe\">GetNameSpace method<\/font><\/a> from the application object. The <b>GetNameSpace<\/b> method requires a single parameter that is always &ldquo;mapi.&rdquo; This is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$namespace<span>&nbsp; <\/span>= $Outlook.GetNameSpace(&#8220;mapi&#8221;)<\/font><\/p>\n<p class=\"MsoNormal\">After you have a <b>namespace<\/b> object, you can use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219900.aspx\"><font face=\"Segoe\">GetDefaultFolder method<\/font><\/a> to return a specific folder. You use the <b>OlDefaultFolders<\/b> enumeration to specify the folder you wish to return. To connect to the inbox, use the <b>olFoderInbox<\/b>. Other folders are listed in the MSDN article that discusses the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.office.interop.outlook.oldefaultfolders.aspx\"><font face=\"Segoe\">OlDefaultFolders enumeration<\/font><\/a>. The returned folder is stored in the <b>$folder<\/b> variable as shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$folder = $namespace.getDefaultFolder($olFolders::olFolderInbox)<\/font><\/p>\n<p class=\"MsoNormal\">The <b>items<\/b> property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176362.aspx\"><font face=\"Segoe\">folder object<\/font><\/a> has the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176540.aspx\"><font face=\"Segoe\">items property<\/font><\/a> that returns an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219950.aspx\"><font face=\"Segoe\">items collection<\/font><\/a> object that represents all the items in the specified folder. In our script, because we are connected to the inbox, the items collection returns instances of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219953.aspx\"><font face=\"Segoe\">Outlook MailItem objects<\/font><\/a>. It is important to know which types of objects are returned by the <b>Items<\/b> property because it will determine which properties are available for filtering, and which methods are available for processing the items in the collection. <b>MailItem<\/b> object <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb208403.aspx\"><font face=\"Segoe\">properties<\/font><\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb277394.aspx\"><font face=\"Segoe\">methods<\/font><\/a> are documented on MSDN. The collection of <b>MailItem<\/b> objects are stored in the <b>$items<\/b> variable, as shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$items = $folder.Items<\/font><\/p>\n<p class=\"MsoNormal\">A single string is displayed on the Windows PowerShell console to indicate which results are being shown:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">&#8220;The following items are marked for followup:&#8221;<\/font><\/p>\n<p class=\"MsoNormal\">To work through the collection of <b>MailItem<\/b> objects, the <b>foreach<\/b> statement is used. The <b>$item<\/b> variable is used as enumerator as the script works through the collection of objects. If the <b>flagStatus<\/b> is equal to 2, it means that the mail item has been flagged for follow up:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">foreach($item in $items)<br>{<br><span>&nbsp;<\/span>if($item.flagStatus -eq 2)<br><span>&nbsp;&nbsp; <\/span>{ $item.subject } <br>}<\/font><\/p>\n<p class=\"MsoNormal\">When the script runs, the results shown in the following image are displayed in the Windows PowerShell 2.0 ISE:<\/p>\n<p class=\"Fig-Graphic\"><span><img decoding=\"async\" title=\"Image of the results of running the script\" alt=\"Image of the results of running the script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1214\/hsg-12-14-09-01.jpg\" width=\"600\" height=\"399\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1214\/hsg-12-14-09-01.jpg\"><font face=\"Segoe\"><\/font><\/a><\/span><\/p>\n<p class=\"MsoNormal\"><br>MS, that is all there is to using Windows PowerShell to retrieve follow-up e-mail messages. Microsoft Outlook Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I don&rsquo;t know about you, but I get a ton of e-mail every day. Of course, I use inbox rules to filter my e-mail, but one thing that I seem to miss is e-mail that has been marked for follow-up. I would love to see a Windows PowerShell script that will troll [&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,45],"class_list":["post-51803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I don&rsquo;t know about you, but I get a ton of e-mail every day. Of course, I use inbox rules to filter my e-mail, but one thing that I seem to miss is e-mail that has been marked for follow-up. I would love to see a Windows PowerShell script that will troll [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51803","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=51803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51803\/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=51803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}