This blog post is part of my second series about powershell classes, this time in collaboration with 4sysops.com. This blog post focuses on powershell class inheritance. This series has a total of 6 posts that covers everything that needs to be known about powershell classes. You will learn how to use powershell classes to manage your Active Directory going gradually through complex concepts until you are a pro with classes. I recommend you follow each step chronologically since we are building a class from bottom up, and keep adding new blocks of code in each chapter. Quick reference: Introduction (here) Properties (here) Methods (here) Constructors (here) Inheritance (here) This post Polymorphism (here) All the examples that are present in this series are available on my github page here. (Exact reference to the file will be provided throughout the series) Alternatively, I recommend you also read the first series I wrote on powershell classes. It will give you another concrete example, and walk you through some things that might not be fully covered in this series.

Introduction to class inheritance

The way we have looked at powershell classes until know, we can actually say that it is a great feature, but that everthing that we have learned so far could already be achieved in a way or in another before the famous keyword Class {} showed up in Powershell 5.0. But there is are three features that powershell didn’t had until classes were introduced in powershell 5.0: Constructors, inheritance and polymorphism. I have already talked about constructors here and here . In this article I will focus on powershell class inheritance, and what it actually brings to the game. I will be covering Polymorphism here, but be sure to understand everything on this page before you continue your journey towards powershell classes. PowerShell Class inheritance is by far the biggest advantage in using Powershell classes (Or classes in general actually). With powershell class inheritance, you will be able to create a hierarchy between your powershell objects. Each child will be able to inherit properties and methods from a parent class. These inherited properties and methods are then available to the child class just as it was their own.

In other words, you write write code once, and use it twice (or more!). This give great flexibility, make our programs more robust, less error prone and make our code twice as reusable as before! Inheritance also open’s the door to polymorphism (I cover Polymorphism in the next part of this series). Implementing Powershell class inheritance is extremely simple! There are some things that could throw you off if you start with classes, so be sure to keep reading to get all of it! why should you start to use powershell class inheritance? Fields and methods from parent class are available (inherited) in the child class(es). (Write code once , use it twice ) Avoid duplication of code ( No more copy/pasting! ) Increases flexibility. ( Changes in a parent class are immediately available in a inherited child class ). Less error prone (Adding a change is less drastic, since it has to be done only once in the parent class). Enforces a better code structure. (Creates a hierarchy between objects – and will have to make you think a little bit of the implementation before you start to write your code) Inheritance is what brings you ultimately to Polymorphism

Basic concepts of class inheritance