{"id":73441,"date":"2015-09-10T00:01:00","date_gmt":"2015-09-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/09\/10\/use-inheritance-in-powershell-5-classes\/"},"modified":"2019-02-18T09:35:18","modified_gmt":"2019-02-18T16:35:18","slug":"use-inheritance-in-powershell-5-classes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-inheritance-in-powershell-5-classes\/","title":{"rendered":"Use Inheritance in 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 using inheritance with Windows PowerShell 5.0 classes in Windows 10.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk about class inheritance with Windows PowerShell 5.0 in Windows 10.<\/p>\n<p>In <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/introduction-to-powershell-5-classes\/\" target=\"_blank\">Introduction to PowerShell 5 Classes<\/a>, I talked about class design and I mentioned how important it was to spend time designing classes before you begin the writing. As it turns out, I did not really do that. In <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a>, I designed a <b>Car<\/b> class, but you know what? Nearly every property, method, and enum that I designed for the <b>Car <\/b>class, also applies to a truck, to a motorcycle, to a recreational vehicle, and maybe even to a boat, a quad runner, or even an airplane. Even, if I don&rsquo;t know if a quad runner has a vehicle identification number (VIN), it certainly has a make, a model, a year, and a color.<\/p>\n<p>So, instead of having created a <b>Car<\/b> class, I should have created a <b>Vehicle<\/b> base class. Now, the sole purpose of the <b>Vehicle<\/b> base class would be to define properties that all vehicles would possess. In fact, if I do something like the following&#8230;<\/p>\n<p style=\"margin-left:30px\">Make<\/p>\n<p style=\"margin-left:30px\">Model<\/p>\n<p style=\"margin-left:30px\">Year<\/p>\n<p style=\"margin-left:30px\">Color<\/p>\n<p>&#8230;I could even use those same four properties to describe a bicycle.<\/p>\n<p>In Active Directory Domain Services (AD&nbsp;DS) we have the following:<\/p>\n<p style=\"margin-left:30px\">Person &gt; Organizational Person &gt; User &gt; Computer<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> As an aside, there is a good site on MSDN that documents the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms680987(v=vs.85).aspx\" target=\"_blank\">Computer class<\/a> and shows from where each attribute derives. This is also why in the old days, when doing LDAP types of queries, if I wanted to find only users, I would query for instances of the <b>User<\/b> class, but not instances of the <b>Computer<\/b> class. If I only queried for <b>Users<\/b>, I would also get <b>Computers<\/b> because they derive from <b>Users<\/b>.<\/p>\n<h2>Create a Vehicle class<\/h2>\n<p>The first thing I want to do is create a base class, which will be called <b>Vehicle<\/b>. This is the same as when I created my <b>Car<\/b> class. I use the <b>Class<\/b> keyword, specify a name and a script block, and create my properties.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> For more information about creating a simple PowerShell 5.0 class, see <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a>.<\/p>\n<p>Here is my <b>Vehicle<\/b> base class:<\/p>\n<p style=\"margin-left:30px\"><strong>Class Vehicle<\/strong><\/p>\n<p style=\"margin-left:30px\">{<\/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; [string]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<h2>Inherit the base class<\/h2>\n<p>Now I want to create my <b>Car<\/b> class. This time, I will inherit the four common properties from the base <b>Vehicle<\/b> class. This is very easy to do. I again use the <b>Class<\/b> keyword and specify a name, but I also use a colon and specify the class that I inherit from. In the script block, I add the new properties that make my new class unique. Here is an example of creating the <b>Car<\/b> class by inheriting from the <b>Vehicle<\/b> class:<\/p>\n<p style=\"margin-left:30px\"><strong>Class Vehicle<\/strong><\/p>\n<p style=\"margin-left:30px\">{<\/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; [string]$make<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [String]$color<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">Class Car : Vehicle<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;[string]$vin<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Now when I call the static <b>New()<\/b> method from my <b>Car<\/b> class, I get all of the properties&mdash; those from the <b>Vehicle<\/b> base class and the new <b>VIN<\/b> property I added in my <b>Car<\/b> class. Here is the method call I use:<\/p>\n<p style=\"margin-left:30px\">[car]::new()<\/p>\n<p>The code and the output from the code are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-10-15-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-10-15-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now I can use any of the techniques I used in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-5-create-simple-class\/\" target=\"_blank\">PowerShell 5: Create Simple Class<\/a> to populate the properties of my new instance of a <b>Car<\/b> class.<\/p>\n<p>Now you have a good understanding about using class inheritance. Windows PowerShell 5.0 Class Week&nbsp;2 will continue 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 using inheritance with Windows PowerShell 5.0 classes in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk about class inheritance with Windows PowerShell 5.0 in Windows 10. In Introduction to PowerShell 5 Classes, I talked about class design and I mentioned how [&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,609,3,608,45],"class_list":["post-73441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-classes","tag-powershell-5","tag-scripting-guy","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using inheritance with Windows PowerShell 5.0 classes in Windows 10. Microsoft Scripting Guy, Ed Wilson, is here. Today I want to talk about class inheritance with Windows PowerShell 5.0 in Windows 10. In Introduction to PowerShell 5 Classes, I talked about class design and I mentioned how [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73441","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=73441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73441\/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=73441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}