Now that you’re familiar with PowerShell classes, as well as its programming concepts and terminology, it is time to show you some other benefits to using PowerShell classes. In this article, I’m going to talk about constructors and how you can use different constructors to create instances of classes with different members. I’m also going to introduce another new concept for classes called inheritance, along with showing some practical examples.

Previously on PowerShell classes…

I started out by introducing PowerShell classes and general class programming concepts. Here I explored the similarities of a class to a PowerShell object, for example, classes have members – both properties and objects – just like an object in PowerShell. I also introduced creating a new class named Rock and assigning properties to it. Next, I discussed the use of a construct known as an enum to define a custom type for a property and compared enums to the PowerShell parameter validation attribute ValidateSet. Lastly, I showed how to put the three together. So the Rock class now has properties and methods and how some properties make use of enum types.

Default Constructors

In the previous articles, I have already shown how to use the default constructor to create an instance of a class. But what does “constructor” mean? A constructor is a method named the same name as the class. If no constructors are specified within the class, it automatically gets a default constructor with no parameters that I can use to create an instance of a class, like seen in the previous article.

For example, I created an instance of a Rock by using the following:

This creates an instance of a Rock. I am using the default constructor because I have not yet defined any constructors in my class. The instantiation occurs when I call the new() method with no parameters and I will get an instance of a rock with unassigned or default properties.

Overloading constructors

I can create an overload for the constructor. The overload allows me to set default values for properties right in the new method itself. This spares me one or more lines of code in assigning properties later. If I want to continue using the constructor with no parameters also, I need to explicitly define it in the class.



1 Rock ( ) { }



In the next example, I am creating an overload for a constructor of the Rock class that has one parameter, Size.



1 2 3 Rock ( [ size ] $Size ) { $this . Size = $size }



What exactly does this mean? It means that if I instantiate a class of Rock with one parameter, that one parameter must be the Size parameter than it must be of type Size. A successful instantiation looks like this:

Adding Additional Overloads

What happens if I try to give it a different parameter, maybe a color instead? When I do that, I receive an error saying that the string is not contained in the enum “Size”. This is also another good argument for using enums! If I had used a string type for Size, “Red” would have been acceptable.

Conversely, what happens if I wanted to assign both Size and Color? With the current constructors I have defined, I would also receive an error trying to create this instance. Notice that it is an error about the overload itself. In this case, it is looking for an overload that accepts two arguments. However, I have not created one that accepts two arguments yet.

1 2 3 4 Rock ( [ size ] $Size , [ string ] $Color ) { $This . Size = $Size $This . Color = $Color }

Here is the constructor for a Rock class that accepts two arguments:



Now, I can create instances of Rocks with zero, one, or two parameters:

Subscribe to Petri Newsletters Office 365 Insider Our Petri Office 365 Insider is dedicated to sharing detailed knowledge from top Office 365 experts. Delivered once a month to your inbox. All Newsletters Petri.com may use your contact information to provide updates, offers and resources that may be of interest to you. You can unsubscribe at any time. To learn more about how we manage your data, you can read our Privacy Policy and Terms of Service. !Already a Petri.com member? Login here for 1-click registration.

What Is Inheritance?

However, for the 2-parameter overload, the parameters must be defined in that exact order – size first, then color. By reversing them, PowerShell will attempt to assign “Red” to Size and I receive the same error that I received in one of the previous examples.

Classes in PowerShell, just like in other object-oriented programming languages, can be hierarchical. This means that I can build a subclass from a parent class and allow the subclass to inherit the properties and members from the parent class. For example, if I had a parent class named “ConstructionMaterials”, that class would contain some properties, likely some of the same properties of the Rock class. A rock is a specific type of construction material, so it inherits the properties from the ConstructionMaterials class. It can also have its own properties. Likewise, I could create other subclasses from ConstructionMaterials, like Brick and Wood. In the following example, I have taken the Rock class and created a ConstructionMaterials class, minimizing the properties.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 enum Size { Small Medium Large } class ConstructionMaterial { [ string ] $Color [ string ] $Shape [ Size ] $Size [ int ] $Location [ void ] ShowLocation ( ) { $Filler = ' ' for ( $i = 0 ; $i -lt $This . Location ; $i ++ ) { $filler += ' ' } write-host "$($Filler)*" } [ void ] ThrowIt ( [ int ] $arg ) { $This . Location += $arg } [ void ] SmashIt ( ) { if ( $This . Size -eq "Large" ) { $This . Size = "Medium" } elseif ( $This . Size -eq "Medium" ) { $This . Size = "Small" } } }



Then, I will create a Rock class that is a subclass of ConstructionMaterial using the syntax:



1 < em > class < / em > subclassName : baseClassName { }



Lastly, I will verify that the Rock class is a subclass of ConstructionMaterial.

Adding Additional Properties to a Subclass

Perhaps, I want the Rock subclass to also have the Luster, Texture, and Pattern properties that I defined in the original rock class. In order to accomplish that, we need to load the enum for the values for Luster into memory by either dot-sourcing it or adding it directly to the class file. Next, we create a new Rock class that contains the additional properties.



1 2 3 4 5 class Rock : ConstructionMaterial { [ string ] $Texture [ string ] $Pattern [ Luster ] $Luster }



If I create an instance of a Rock, it contains the properties of the base class (Color, Shape, Size, and Location). In addition, it also contains the Rock-specific properties (Texture, Luster, and Pattern.)

Practice Makes Perfect (Sense)

I have shown how to use the default constructor to create an instance of a class without defining anything specifically in the class itself. I have also shown adding constructor overloads to explicitly define values within the instance of the class. In addition, I have talked about inheritance and how to create base classes and subclasses. Now, it’s your turn. Look for ways you can incorporate classes into your everyday programming. Or, perhaps you might want to just play around with building a class for fun and to continue building your knowledge. Either way, it’s a great way to become familiar with the programming concepts of classes, which will bleed over into other object-oriented languages as well.