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:

  1. Introduction (here)
  2. Properties (here)
  3. Methods (here)
  4. Constructors (here)
  5. Inheritance (here) This post
  6. 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?

  1. Fields and methods from parent class are available (inherited) in the child class(es). (Write code once, use it twice)
  2. Avoid duplication of code (No more copy/pasting!)
  3. Increases flexibility. (Changes in a parent class are immediately available in a inherited child class).
  4. Less error prone (Adding a change is less drastic, since it has to be done only once in the parent class).
  5. 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)
  6. Inheritance is what brings you ultimately to Polymorphism

Basic concepts of class inheritance

For those new to object oriented programming (also called OOP), you will need to know that OOP is based on four pillars:

  1. Abstraction
  2. Inheritance
  3. Polymorphism
  4. Encapsulation

Class inheritance is a big part of object oriented programming, and this functionality just got available to us.

As the name is suggesting, class inheritance will allow us to inherit (to receive) existing properties and methods from a parent class. The global idea is to use the power of class inheritance to gain time in coding, by thinking the model through before we actually start to write code, and only write the code once and make it inherit to one or several child classes.

To demonstrate this behavior, we will start with a class called employee, which creates new users in our active directory.

Have a look at the class here under, but do not spend too much time reading the details of it, because in this chapter we are only interested in the concepts of inheritance. The details of some of the methods that allow us to use polymorphism are available on my other post here.

Note: The following examples require at least PowerShell version 5.0 The Active Directory module also needs to be present.

We can create an instance of our class, and have a look at its contents:

powershell class inheritance properties and methods

We can see our methods (in green) and our properties (in yellow).

Our static method is also visible using the following command:

powershell class inheritance static methods

We see that our GetNewUserName static method is present.

Class Inheritance syntax

Now that we know how to see the properties and methods that we have access to, lets create our first inherited class.

To do so, the syntax is the following:

Simply adding “: ” behind our new class, to automatically ‘activate’ inheritance.

Properties and methods inheritance

What does this actually brings?

Creating an instance, and having a look at its properties will show us the following:

class inheritance inherited fields

class inherited static method

We see here, that even though our inherited ExternalEmployee class was actually empty, we still got access to all of our properties and methods from our parent class ‘employee’, without doing a single effort.

One last thing to note is that constructors are not inherited.

This is how we can easily find out if constructors are inherited or not:

powershell class constructors are not inherited

We can clearly see, that we have our two constructors in our parent class “employee”, and that we have only one parameter-less constructor in our child class “ExternalEmployee”.

To learn more in depth information about constructors and class inheritance in general, I recommend you read my other blog post here.

That was it for today. There is still one last step to complete our learning process around powershell classes inheritance, and it is called Polymorphism.

Read the last article about polymorphism of this second series about Classes here.