Understanding Classes in PowerShell – Part 01 – Defining classes

Starting in Windows PowerShell 5.0, Windows PowerShell adds language for defining classes and other user-defined types. It is achieved by using the similar syntax and semantics being in use by other major object oriented programming languages. The goal is to enable developers (which I know, hate default syntax format and looking to build on their existing knowledge) and IT Professionals (to use more of their knowledge and make use of more formal coding mechanisms to achieve better code) to embrace Windows PowerShell for a wider variety of use cases. The use cases can be Desired State Configuration (DSC) resources, Generate and Handle exceptions, Introducing custom types, refactoring and re-using same code etc. This blog post is intended to cover PowerShell classes and their implementation in detail. This will be spread over multiple posts as this is a huge topic.

What is a Class

We can think of Classes as models or blueprints. To use a Class, we create a special type of variable known as an Object. Or we can say that an Object is an instance of a Class.

You might want to think of a class as a way to create a specification of variables, functions, and other properties (e.g. a template). Now to use this model, we must create an instance of the specification (object). Similarly, this is the same idea where a contractor can use a blueprint to build multiple houses. Once created, an object has access to properties and methods defined by its class. Methods are nothing but the behavior/operation/actions of an Object.

Define Class

The first thing we need to do is to use the Class keyword. This is what creates the class. The syntax is exactly the same as creating a function:

Keyword Name
Opening brace
Closing brace

And there is stuff in the middle. So, to create the Dog class,we can type:

Class Dog
{
  # Properties and methods go in here.
}

Then we can start adding properties.

Adding Properties to Class

A dog has several properties like name, owner, breed, etc. While defining properties, we can either separate by using semi-colon(;) or end-of-line (carriage return line feed). The default terminator is end-of-line (carriage return line feed). While defining properties, it is a good idea to define data type of each property. This data type can be either one of the built-in data types such as int, int32, long, string, datetime, etc. or one of the custom data types. We can simply define properties like below:

Class Dog{
 [String] $Name
 [String] $Breed
 [String] $OwnerName
 [String] $OwnerAddress
 [DateTime] $RegistrationDate
 [long] $RegistrationNumber
}

If you don’t define the data type of property, it would default to System.Object and would be pretty much useless. For example, below is also a valid syntax:

Class Dog2{
 $Name
 $Breed
}

However, you should always strive to define the datatype in advance for each property so that you can have access to the properties and methods related to that data type. Values for some of these properties is common to every dog like number of legs. So in such as case we can use static to make sure to define its value at time of declaring the class:

Class Dog{
 [String] $Name
 [String] $Breed
 [String] $OwnerName
 [String] $OwnerAddress
 [DateTime] $RegistrationDate
 [long] $RegistrationNumber
 static [int] $NumberOfLegs = 4

}

Static keyword just means that it is always available. You don’t have to declare it to get it. Also wherever possible, define the default value of the property. For example, we have declared the default number of legs for a dog is 4. Do remember that:
1. If a value is not declared, the property will not always be null. It may or may not be null.
2. The default value of the properties data type determines the value.

For example, the default value for a int datatype is 0 and for datetime object is 1/1/0001 12:00:00 AM.

We can also declare access modifiers for properties such as public or hidden. By default, a property is public. Hidden means that this property will be hidden. For example,

# Create hidden property
hidden [String] $PetName

Create an instance of the Class

To use a class, we must instantiate an object unless using static properties or methods. More on that later. Most commonly, this is done using the new() static method or the New-Object Command. For example, below lines creates an object of Class Dog:

$myDog = New-Object -TypeName Dog

You can see the default value generated for each property by simply using $myDog:

PS C:\Windows\system32> $myDog

Name : 
Breed : 
OwnerName : 
OwnerAddress : 
RegistrationDate : 1/1/0001 12:00:00 AM
RegistrationNumber : 0

We can use Static property operator :: to view static properties:

PS C:\Windows\system32> $myDog::numberoflegs
4

We can access properties using dot (.) notation. It can be used for both getting and setting values. For example:

PS C:\Windows\system32> $myDog.Name = "sonna"
PS C:\Windows\system32> $myDog.OwnerName = "Indira Thakur"
PS C:\Windows\system32> $myDog.Name
sonna

That’s how you create a simple class in Windows PowerShell 5.0. We’ll continue to explore more on this topic in further posts. Part 02 of this series can be read here.

One thought on “Understanding Classes in PowerShell – Part 01 – Defining classes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s