{"id":73721,"date":"2015-08-27T00:01:00","date_gmt":"2015-08-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/27\/working-with-enums-in-powershell-5\/"},"modified":"2019-02-18T09:35:27","modified_gmt":"2019-02-18T16:35:27","slug":"working-with-enums-in-powershell-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/working-with-enums-in-powershell-5\/","title":{"rendered":"Working with Enums in PowerShell 5"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about working with enums in Windows PowerShell 5.0 in Windows 10.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked about the new stealth feature in Windows PowerShell 5.0 in Windows 10 that permits me to easily create an enum. Enums are great because they provide a concise way for me to check parameters, a great way to make code easier to read, and many other things.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> Today&#039;s post continues <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/new-powershell-5-feature-enumerations\/\" target=\"_blank\">New PowerShell 5 Feature: Enumerations<\/a>. You should read it prior to reading today&#039;s post.<\/p>\n<p>Awhile back, I wrote a function called <b>Get-EnumValues<\/b>, which has since come to reside in my Windows PowerShell profile. I talk about this function in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/one-of-my-favorite-powershell-functions\/\">One of My Favorite PowerShell Functions<\/a>. What is cool about the function is that it accepts an enum, and then it returns an array of the enumeration names and the associated numeric values.<\/p>\n<p>This is an extremely powerful and helpful tool to have around when one is working with enums (whether self-created or one of the thousands of enums that reside in the .NET Framework).&nbsp;<\/p>\n<p>Here is the function:<\/p>\n<p style=\"margin-left:30px\">Function get-enumValues<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;# get-enumValues -enum &quot;System.Diagnostics.Eventing.Reader.StandardEventLevel&quot;<\/p>\n<p style=\"margin-left:30px\">Param([string]$enum)<\/p>\n<p style=\"margin-left:30px\">$enumValues = @{}<\/p>\n<p style=\"margin-left:30px\">[enum]::getvalues([type]$enum) |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p style=\"margin-left:30px\">$enumValues.add($_, $_.value__)<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">$enumValues<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>To use the function, I simply call <b>Get-EnumValues<\/b> and specify the name of the enum in quotation marks:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; get-enumValues -enum &quot;fruit&quot;<\/p>\n<p style=\"margin-left:30px\">Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">Apple &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;29&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>One of the cool things I can use is the <b>[enum]<\/b> type accelerator, and I can use that to display the values that are defined inside an enum. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; [enum]::GetValues([type]&quot;fruit&quot;)<\/p>\n<p style=\"margin-left:30px\">Apple<\/p>\n<p style=\"margin-left:30px\">Pear<\/p>\n<p style=\"margin-left:30px\">Kiwi<\/p>\n<p>But it does not do what I would expect. I would expect to see the numeric values, not the enumeration names or properties. As shown here, when I reference a property via the enumeration, it simply displays the name again:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; [fruit]::Apple<\/p>\n<p style=\"margin-left:30px\">Apple<\/p>\n<p>The secret is knowing that there is a <b>value__<\/b> property associated with each enumeration. When I call that, then I get the numeric value:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; [fruit]::Apple.value__<\/p>\n<p style=\"margin-left:30px\">29<\/p>\n<p>That is the &ldquo;trick&rdquo; that my <b>Get-EnumValues<\/b> function uses to create the hash table it displays.<\/p>\n<h2>Create new enum based on existing enum<\/h2>\n<p>One of the really cool things I can do with enums is use them to create new enums. To continue my example from yesterday, I can create more fruit. My original enum is shown here:<\/p>\n<p style=\"margin-left:30px\">Enum Fruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Apple = 29<\/p>\n<p style=\"margin-left:30px\">&nbsp;Pear = 30<\/p>\n<p style=\"margin-left:30px\">&nbsp;Kiwi = 31<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Now I decide that I want to create more fruit. So I call my new enum <b>MoreFruit<\/b>:<\/p>\n<p style=\"margin-left:30px\">Enum MoreFruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>To combine a pear and an apple, I get a Papple. It is as simple as calling the pear enumeration and the apple enumeration and adding them together. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Papple = [fruit]::Pear + [fruit]::Apple<\/p>\n<p>I can also combine a kiwi and an apple and get a Kapple:<\/p>\n<p style=\"margin-left:30px\">Kapple = [fruit]::Kiwi + [fruit]::Apple<\/p>\n<p>I imagine that I could combine a kiwi, a pear, and an apple and get a KaPapple, and this is what I do:<\/p>\n<p style=\"margin-left:30px\">KaPapple = [fruit]::Kiwi + [fruit]::Pear + [fruit]::Apple<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">Enum Fruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Apple = 29<\/p>\n<p style=\"margin-left:30px\">&nbsp;Pear = 30<\/p>\n<p style=\"margin-left:30px\">&nbsp;Kiwi = 31<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum MoreFruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Papple = [fruit]::Pear + [fruit]::Apple<\/p>\n<p style=\"margin-left:30px\">&nbsp;Kapple = [fruit]::Kiwi + [fruit]::Apple<\/p>\n<p style=\"margin-left:30px\">&nbsp;KaPapple = [fruit]::Kiwi + [fruit]::Pear + [fruit]::Apple<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>When I run it, I can access the KaPapple from my <b>MoreFruit<\/b> enum:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; [morefruit]::KaPapple<\/p>\n<p style=\"margin-left:30px\">KaPapple<\/p>\n<p style=\"margin-left:30px\">I can also see the numeric value of the KaPapple:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\mredw&gt; [morefruit]::KaPapple.value__<\/p>\n<p style=\"margin-left:30px\">90<\/p>\n<p>I see that it added 29, 30, and 31 together to get 90. I can now use my <b>Get-EnumValues<\/b> function to see what else is in my <b>MoreFruit<\/b> enum. The output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-27-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-27-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using enums. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about working with enums in Windows PowerShell 5.0 in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked about the new stealth feature in Windows PowerShell 5.0 in Windows 10 that permits me to easily create an enum. Enums are great because they provide a [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[148,609,3,4,608,45],"class_list":["post-73721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-enum","tag-powershell-5","tag-scripting-guy","tag-scripting-techniques","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about working with enums in Windows PowerShell 5.0 in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday, I talked about the new stealth feature in Windows PowerShell 5.0 in Windows 10 that permits me to easily create an enum. Enums are great because they provide a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73721","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=73721"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73721\/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=73721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}