{"id":51793,"date":"2009-12-15T00:01:00","date_gmt":"2009-12-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/15\/hey-scripting-guy-how-can-i-tell-which-outlook-rules-i-have-created\/"},"modified":"2009-12-15T00:01:00","modified_gmt":"2009-12-15T00:01:00","slug":"hey-scripting-guy-how-can-i-tell-which-outlook-rules-i-have-created","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-tell-which-outlook-rules-i-have-created\/","title":{"rendered":"Hey, Scripting Guy! How Can I Tell Which Outlook Rules I Have Created?"},"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>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><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 it seems that I am getting overwhelmed at work with e-mail. In the old days, e-mail was not much of a problem and it was a great way to communicate. Today, I get so much e-mail that it would actually take all day for me to read it. The problem is that some of it is actually important. I have created a bunch of rules that will delete certain e-mail messages, move other messages to certain folders, and play a specific sound if I get an e-mail message from my boss. Now the problem is that I have too many rules, and some of them actually conflict with one another. <\/p>\n<p class=\"MsoNormal\">I need a good way to visualize which rules I have so that I can determine if I still need the rule. Could you write a Windows PowerShell script that would tell me which Microsoft Outlook inbox rules I have created? <\/p>\n<p class=\"MsoNormal\">&#8212; PC<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/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 PC, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I spent the weekend out in my woodworking shop making a table for my mother-in-law. It is a simple little table, but it took an inordinate amount of time to hand cut the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mortise_and_tenon\"><font face=\"Segoe\">mortise and tenon joints<\/font><\/a> for the apron. The nice thing about hand cutting woodworking joints is that it is quiet. I can listen to classical music on my Zune HD because I have a docking station in my wood shop, and it is very peaceful. There is something relaxing about the sound of a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Plane_(tool)\"><font face=\"Segoe\">sharp plane<\/font><\/a> as it scrapes across the wood as it takes .001 off an inch of the side of a tenon for a precise fit. <\/p>\n<p class=\"MsoNormal\">I get a similarly peaceful feeling from a finely crafted Microsoft Office Outlook rule that deletes thousands of inane e-mails from my inbox. As with fitting a hand-cut mortise and tenon joint, one does not want to remove too much&mdash;just exactly enough. I imagine that FDISK would solve many of one&rsquo;s e-mail problems, but that would be akin to using a chain saw on my mother-in-law&rsquo;s table. It would remove Office Outlook, Microsoft Windows, and all of my data along with it. <\/p>\n<p class=\"MsoNormal\">To get a precisely fitting joint, I make a couple passes with my plane, and then I test fit the leg. To get Outlook inbox rules that are a precise fit for your needs, you create the rule and inspect the results. Having a list of inbox rules is the first step in the right direction. <\/p>\n<p class=\"MsoNormal\">The complete ListOutLookRules.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ListOutLookRules.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><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>$rules = $outlook.session.DefaultStore.GetRules()<br>$rules |<br>Sort-Object -Property ExecutionOrder |<br>Format-Table -Property Name, ExecutionOrder, Enabled, isLocalRule -AutoSize<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The ListOutLookRules.ps1 script will display all rules that you have created in Microsoft Outlook. The Outlook Rules and Alerts Wizard, seen in the following image, will also display the rules you have created:<\/p>\n<p class=\"Fig-Graphic\"><span><u><font color=\"#0066cc\"><img decoding=\"async\" title=\"Image of Outlook Rules and Alerts Wizard\" alt=\"Image of Outlook Rules and Alerts Wizard\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1215\/hsg-12-15-09-01.jpg\" width=\"600\" height=\"491\"><\/font><\/u><\/span><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">The first thing that is done in the ListOutLookRules.ps1 script is adding the <b>microsoft.office.interop.outlook<\/b> assembly to the current Windows PowerShell session. Because the <b>Add-Type<\/b> cmdlet is used to do this and the <b>Add-Type<\/b> cmdlet only exists on Windows PowerShell 2.0, the <b>#Requires &ndash;version 2.0<\/b> tag is used to prevent the script from running on Windows PowerShell 1.0 machines. For more information about using the Add-Type cmdlet, see <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/12\/14\/hey-scripting-guy-december-14-2009.aspx\">yesterday&rsquo;s Hey, Scripting Guy! article<\/a>. <\/p>\n<p class=\"MsoNormal\">The <b>Add-Type<\/b> command is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">#Requires -version 2.0<br>Add-Type -AssemblyName microsoft.office.interop.outlook <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next, the <b>OlDefaultFolders<\/b> enumeration is created. The <b>OlDefaultFolders<\/b> enumeration will be used with the <b>GetDefaultFolder<\/b> method to make a connection to the user&rsquo;s inbox. The enumeration is stored in the <b>$olFolders<\/b> variable, as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$olFolders = &#8220;Microsoft.Office.Interop.Outlook.OlDefaultFolders&#8221; -as [type]<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is now time to create the <b>Outlook.Application<\/b> COM object. To do this use the <b>New-Object<\/b> cmdlet with the <b>&ndash;ComObject<\/b> parameter. The resulting application object is stored in the <b>$outlook<\/b> variable, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$outlook = New-Object -ComObject Outlook.Application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You will also need a <b>namespace<\/b> object. To obtain a <b>namespace<\/b> object, use the <b>GetNameSpace<\/b> method from the <b>Outlook.Application<\/b> COM object. The resulting <b>namespace<\/b> object is stored in the <b>$namespace<\/b> variable, as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$namespace<span>&nbsp; <\/span>= $Outlook.GetNameSpace(&#8220;mapi&#8221;)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To connect to the mailbox, you use the <b>GetDefaultFolder<\/b> method from the <b>namespace<\/b> object. The <b>GetDefaultFolder<\/b> method receives the enumeration that was previously stored in the <b>$olFolders<\/b> variable. The resulting <b>folder<\/b> object is stored in the <b>$folder<\/b> variable, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$folder = $namespace.getDefaultFolder($olFolders::olFolderInbox)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the application object, the namespace, and the connection to the mailbox have been established, you can call the <b>GetRules<\/b> method to return the mailbox rules. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb175104.aspx\"><font face=\"Segoe\">Session property<\/font><\/a> from the <b>Application<\/b> object returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb219955.aspx\">NameSpace object<\/a> that represents the current session. The <b>NameSpace<\/b> object has the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb175604.aspx\"><font face=\"Segoe\">DefaultStore property<\/font><\/a> that returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176405.aspx\"><font face=\"Segoe\">Store object<\/font><\/a> that represents the default store for the Outlook profile. It is the <b>store<\/b> object that has the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb207562.aspx\"><font face=\"Segoe\">GetRules method<\/font><\/a>. The <b>GetRules<\/b> method returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176417.aspx\"><font face=\"Segoe\">Rules Object<\/font><\/a> that is made up of a collection of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176397.aspx\"><font face=\"Segoe\">Rule objects<\/font><\/a> that are present in the current session. The use of the <b>GetRules<\/b> method to return a collection of Outlook <b>rule<\/b> objects is seen here. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$rules = $outlook.session.DefaultStore.GetRules()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Now that you have a collection of rules, you can work with them by using standard Windows PowerShell cmdlets. In the ListOutLookRules.ps1 script,<b> <\/b>they are sent along the Windows PowerShell pipeline as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$rules |<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>Sort-Object<\/b> cmdlet is used to sort the rules by the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb176165.aspx\"><font face=\"Segoe\">ExecutionOrder property<\/font><\/a>. The <b>ExecutionOrder<\/b> property is used by Microsoft Outlook to determine the order in which the rules will be executed. The <b>Sort-Object<\/b> cmdlet is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Sort-Object -Property ExecutionOrder |<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Finally, the <b>Format-Table<\/b> cmdlet is used to generate a table that displays the <b>ExecutionOrder<\/b>, whether the rule is enabled or local, and the name of the rule. The <b>&ndash;autosize<\/b> parameter reduces white space between the columns in the table. The <b>Format-Table<\/b> command is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Format-Table -Property Name, ExecutionOrder, Enabled, isLocalRule &ndash;AutoSize<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the script is run inside the Windows PowerShell 2.0 ISE, the results seen in the following image are displayed on my computer:<\/p>\n<p class=\"Fig-Graphic\"><span><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1215\/hsg-12-15-09-01.jpg\"><font face=\"Segoe\"><\/font><\/a><\/span><\/p>\n<p class=\"Fig-Graphic\">\n<p><img decoding=\"async\" title=\"Image of results in Windows PowerShell ISE of running script\" alt=\"Image of results in Windows PowerShell ISE of running script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1215\/hsg-12-15-09-02.jpg\" width=\"600\" height=\"423\"><\/p>\n<\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">PC, that is all there is to retrieving a list of your Outlook rules. 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\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/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\"><b><span><\/span><\/b>&nbsp;<\/p>\n<p class=\"MsoNormal\"><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I don&rsquo;t know about you, but it seems that I am getting overwhelmed at work with e-mail. In the old days, e-mail was not much of a problem and it was a great way to communicate. Today, I get so much e-mail that it would actually take all day for [&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-51793","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>&nbsp; &nbsp; Hey, Scripting Guy! I don&rsquo;t know about you, but it seems that I am getting overwhelmed at work with e-mail. In the old days, e-mail was not much of a problem and it was a great way to communicate. Today, I get so much e-mail that it would actually take all day for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51793","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=51793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51793\/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=51793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}