September 10th, 2015

Use Inheritance in PowerShell 5 Classes

Doctor Scripto
Scripter

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 important it was to spend time designing classes before you begin the writing. As it turns out, I did not really do that. In PowerShell 5: Create Simple Class, I designed a Car class, but you know what? Nearly every property, method, and enum that I designed for the Car 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’t know if a quad runner has a vehicle identification number (VIN), it certainly has a make, a model, a year, and a color.

So, instead of having created a Car class, I should have created a Vehicle base class. Now, the sole purpose of the Vehicle base class would be to define properties that all vehicles would possess. In fact, if I do something like the following…

Make

Model

Year

Color

…I could even use those same four properties to describe a bicycle.

In Active Directory Domain Services (AD DS) we have the following:

Person > Organizational Person > User > Computer

Note  As an aside, there is a good site on MSDN that documents the Computer class 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 User class, but not instances of the Computer class. If I only queried for Users, I would also get Computers because they derive from Users.

Create a Vehicle class

The first thing I want to do is create a base class, which will be called Vehicle. This is the same as when I created my Car class. I use the Class keyword, specify a name and a script block, and create my properties.

Note  For more information about creating a simple PowerShell 5.0 class, see PowerShell 5: Create Simple Class.

Here is my Vehicle base class:

Class Vehicle

{

    [datetime]$year

    [String]$model

    [string]$make

    [String]$color

}

Inherit the base class

Now I want to create my Car class. This time, I will inherit the four common properties from the base Vehicle class. This is very easy to do. I again use the Class 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 Car class by inheriting from the Vehicle class:

Class Vehicle

{

    [datetime]$year

    [String]$model

    [string]$make

    [String]$color

}

Class Car : Vehicle

{

 [string]$vin

}

Now when I call the static New() method from my Car class, I get all of the properties— those from the Vehicle base class and the new VIN property I added in my Car class. Here is the method call I use:

[car]::new()

The code and the output from the code are shown in the following image:

Image of command output

Now I can use any of the techniques I used in PowerShell 5: Create Simple Class to populate the properties of my new instance of a Car class.

Now you have a good understanding about using class inheritance. Windows PowerShell 5.0 Class Week 2 will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.