{"id":73601,"date":"2015-09-02T00:01:00","date_gmt":"2015-09-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/02\/adding-enums-to-powershell-5-classes\/"},"modified":"2019-02-18T09:35:23","modified_gmt":"2019-02-18T16:35:23","slug":"adding-enums-to-powershell-5-classes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/adding-enums-to-powershell-5-classes\/","title":{"rendered":"Adding Enums to PowerShell 5 Classes"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about adding enums to Windows PowerShell 5.0 classes in Windows 10.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is my birthday week&#8230;almost. It is the kind of thing that one needs to begin making plans and preparations for. I know that the Scripting Wife, aka Teresa, is busy working to make this birthday week one of the best ever&mdash;at least, I hope that is what she is up to.<\/p>\n<p>The cool thing is that after my birthday week, it will be time for her birthday week, so we take pretty much the entire month of September to celebrate birthdays. Then it is October, and there are a couple of conferences we are going to, and then there is November, and then December. Yep, the last four months of the year are always something special.<\/p>\n<p>Then there is January and February and March&hellip;well, you get the idea. If you are into Windows PowerShell, every day of every month of every year is special&mdash;and that is what makes it fun and exciting. In fact, we have been talking to my good friend and Microsoft Evangelist, Blaine Barton, about some way cool stuff for next year in the sunshine state.<\/p>\n<h2>Adding enums<\/h2>\n<p>The advantage of using an enum with your Windows PowerShell 5.0 class is that you have automatic parameter validation in Windows PowerShell. This is essential for solving the old-fashioned problem of the &ldquo;garbage in garbage out&rdquo; maxim of data processing. When applied as a type constraint for a property in a Windows PowerShell class, an enum will generate an error if the value supplied is not a valid enumeration.<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> This is the third post in the series about creating classes in Windows PowerShell&nbsp;5.0. You should read the earlier posts <br \/>&nbsp; &nbsp;prior to reading today&rsquo;s:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-powershell-5-classes\/\" target=\"_blank\">Introduction to PowerShell 5 Classes<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a><\/li>\n<\/ul>\n<p><b>&nbsp; &nbsp;Another Note&nbsp;<\/b> I talked about enums last week. If you need to review enums, you should read:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/new-powershell-5-feature-enumerations\/\" target=\"_blank\">New PowerShell 5 Feature: Enumerations<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/working-with-enums-in-powershell-5\/\" target=\"_blank\">Working with Enums in PowerShell 5<\/a><\/li>\n<\/ul>\n<p>In my car example, it makes sense to use enums for the color of the car and for the maker of the car. I mean afterall, there are only so many colors of cars and so many makers of cars. So if I want to be able to search by color or make, I need to constrain my input. I can say blue and I can find all of my blue cars. But if I have dusty blue, sky blue, ocean blue, greenish blue, light blue, lite blue, lte blue, it quickly becomes a looser proposition.<\/p>\n<p>With Windows PowerShell classes, I cannot actually define my enum inside the Windows PowerShell class, but I can do it outside the class&mdash;and in the same script, I will be able to use the enum inside the class. What do I mean?<\/p>\n<p>Well, let&#039;s quickly define two enums: <b>MakeOfCar<\/b> and <b>ColorOfCar<\/b>:<\/p>\n<p style=\"margin-left:30px\">Enum MakeOfCar<\/p>\n<p style=\"margin-left:30px\">{&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Chevy = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Ford = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Olds = 3<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Toyota = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; BMW = 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum ColorOfCar<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Red = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Blue = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Green = 3<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>How do I use these enums in my Windows PowerShell 5.0 class? I use it as a type\/constraint for the property in my class property section. This is shown here:<\/p>\n<p style=\"margin-left:30px\">[MakeOfCar]$make<\/p>\n<p style=\"margin-left:30px\">[ColorOfCar]$color<\/p>\n<p>Here is my complete script, which includes the <b>Car<\/b> class and the two enums:<\/p>\n<p style=\"margin-left:30px\">Class Car<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$vin<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; static [int]$numberOfWheels = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [int]$numberOfDoors<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [datetime]$year<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$model<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [MakeOfCar]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [ColorOfCar]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum MakeOfCar<\/p>\n<p style=\"margin-left:30px\">{&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Chevy = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Ford = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Olds = 3<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Toyota = 4<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; BMW = 5<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Enum ColorOfCar<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Red = 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Blue = 2<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Green = 3<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>I run the script, and I create a new instance of the <b>Car<\/b> class. I store the returned <b>Car<\/b> object in a variable. I use the <b>NEW()<\/b> static method from the <b>Car<\/b> class. The syntax is shown here:<\/p>\n<p style=\"margin-left:30px\">$a = [car]::New()<\/p>\n<p>This code and the returned object are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now I assign values to the properties. To do this, I use the numeric values of the enumerations. As you can see, it is a straightforward value assignment:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.color = 1<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.color<\/p>\n<p style=\"margin-left:30px\">Red<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.make = 2<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;: 1234<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 0<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Ford<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Red<\/p>\n<p>This is what it looks like in the ISE:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can also use the &ldquo;noun&rdquo; of the enumeration instead of the numeric value. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a.make = &quot;bmw&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a<\/p>\n<p style=\"margin-left:30px\">vin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1234<\/p>\n<p style=\"margin-left:30px\">numberOfDoors : 2<\/p>\n<p style=\"margin-left:30px\">year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/1977 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : mustang<\/p>\n<p style=\"margin-left:30px\">make&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : BMW<\/p>\n<p style=\"margin-left:30px\">color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Red<\/p>\n<p>But, if I use a value that does not exist in the enumeration, it generates an error, as shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-03.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-2-15-03.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to adding enums to Windows PowerShell 5.0 classes. Windows PowerShell Classes Week will continue tomorrow when I will talk about more way 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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding enums to Windows PowerShell 5.0 classes in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. It is my birthday week&#8230;almost. It is the kind of thing that one needs to begin making plans and preparations for. I know that the Scripting Wife, aka Teresa, is [&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":[615,613,609,3,4,608,45],"class_list":["post-73601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-classes","tag-enums","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 adding enums to Windows PowerShell 5.0 classes in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. It is my birthday week&#8230;almost. It is the kind of thing that one needs to begin making plans and preparations for. I know that the Scripting Wife, aka Teresa, is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73601","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=73601"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73601\/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=73601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}