{"id":73741,"date":"2015-08-26T00:01:00","date_gmt":"2015-08-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/26\/new-powershell-5-feature-enumerations\/"},"modified":"2019-02-18T09:35:28","modified_gmt":"2019-02-18T16:35:28","slug":"new-powershell-5-feature-enumerations","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/new-powershell-5-feature-enumerations\/","title":{"rendered":"New PowerShell 5 Feature: Enumerations"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about creating enumerations in Windows PowerShell 5.0.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is nearly a stealth feature in Windows PowerShell 5.0&mdash;there is an <b>Enum<\/b> keyword. Yep, that&#039;s right, there is an <b>Enum<\/b> keyword. I wonder what it does? Probably has something to do with creating enums.<\/p>\n<p>Is this a big deal? You betcha&#8230;as they may say in some places.<\/p>\n<p>Why? Because prior to Windows PowerShell 5.0, you had to basically create an enum by using C# kinds of code, and then adding it as a type. Did it work? Sure it did. Was it Windows PowerShell? Well, in that it actually worked and I wrote it in Windows PowerShell&mdash;sure it was Windows PowerShell.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; For reference, refer to my Hey, Scripting Guy! Blog post <a href=\"\/b\/heyscriptingguy\/archive\/2010\/06\/06\/hey-scripting-guy-weekend-scripter-the-fruity-bouquet-of-windows-powershell-enumerations.aspx\" target=\"_blank\">The Fruity Bouquet of Windows PowerShell Enumerations<\/a>.<\/p>\n<h2>Before PowerShell 5.0<\/h2>\n<p>Before Windows PowerShell 5.0, if I wanted to create an enumeration, I basically wrote inline C# code. Here is an example that creates a simple enumeration that assigns numeric values to three different types of fruit:<\/p>\n<p style=\"margin-left:30px\"># Create-FruitEnum.ps1<\/p>\n<p style=\"margin-left:30px\">$enum = &quot;<\/p>\n<p style=\"margin-left:30px\">namespace myspace<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">public enum fruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">apple = 29, pear = 30, kiwi = 31<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">&quot;<\/p>\n<p style=\"margin-left:30px\">Add-Type -TypeDefinition $enum -Language CSharpVersion3<\/p>\n<p>This is pretty cool code, and the fact that I could create a custom enum in a scripting language was pretty wild. I wrote this code in 2010, and it has worked ever since. In fact, nothing has improved in this regards in the past five years&ndash;&mdash;until Windows PowerShell&nbsp;5.0 came out in Windows&nbsp;10.<\/p>\n<h2>Use the enum keyword<\/h2>\n<p>So, whereas I used to have to create a giant string, with embedded C# code, and then use the Add-Type command to add in C# code, now I have an enum keyword. This makes the code much cleaner, and very easy to use.<\/p>\n<p>I use the enum keyword and assign a name for the enum. I then open and close a script block. This appears here:<\/p>\n<p style=\"margin-left:30px\">Enum Fruit<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Now I simply assign numeric values for each property:<\/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>When I run my script, it creates the enum. I can then access each property as a static property. This is shown here:<\/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 cool thing is that IntelliSense pops up and shows the permissible members:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-26-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-26-15-01.png\" border=\"0\" alt=\" \" \/><\/a><\/p>\n<p>That is all there is to using enumerations in Windows PowerShell 5.0.&nbsp; 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 creating enumerations in Windows PowerShell 5.0. Microsoft Scripting Guy, Ed Wilson, is here. It is nearly a stealth feature in Windows PowerShell 5.0&mdash;there is an Enum keyword. Yep, that&#039;s right, there is an Enum keyword. I wonder what it does? Probably has something to do with creating [&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-73741","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 creating enumerations in Windows PowerShell 5.0. Microsoft Scripting Guy, Ed Wilson, is here. It is nearly a stealth feature in Windows PowerShell 5.0&mdash;there is an Enum keyword. Yep, that&#039;s right, there is an Enum keyword. I wonder what it does? Probably has something to do with creating [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73741","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=73741"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73741\/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=73741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}