Object-Oriented PowerShell

Object-Oriented Programming (OOP) is based around the concept of Objects, where an Object is made up of Attributes, or Variables, that can contain data, and Methods, that describe actions of an Object. Attributes and Methods are collectively known as members of a Class. A person can be used as a real-world example of an Object. A person has Attributes such as height, weight, eye colour, hair colour and so on, as well as Methods or actions such as, walk, talk and eat.

A Class is a blueprint, or template, of an Object that describes its Attributes and Methods. From this template one or more Objects can be created, which is known as instantiating an Object. A Class can be compared to an architect's drawing of a building, which describes how it needs to be built. From this drawing, one or more buildings can be made to the specification of the drawing.

In PowerShell, the basic structure of a Class is as follows.

class ClassName
{

    # Attributes and methods go here.

}

Notice the capitalisation of the first letter of each word in the Class name. This is known as Pascal Case and is the standard method for naming Classes in PowerShell.

Below is an example of a Class called ‘Greeting’, along with how it can be instantiated, using the name ‘greeting’.

class Greeting
{

    # Attributes and methods go here.

}

# Instance of the class Greeting called 'greeting'.
$greeting = New-Object Greeting

Adding Attributes

Attributes are added to Classes in PowerShell in the form of Variables.

class Greeting
{

    # Attributes.
    $message = "Hello World!"

}

# Class instance.
$greeting = New-Object Greeting

# Display the class attribute 'message' in the terminal.
Write-Host $greeting.message

Here an Attribute, ‘message’, has been added to the ‘Greeting’ Class. After the Object has been instantiated, the message is printed out to the terminal.

Setting Attributes

Setting an Attribute of an Object is done in a similar way to accessing it. In this example a ‘name’ Attribute has been added to the ‘Greeting’ Class. This is set to ‘Fred’ and concatenated with the rest of the message before being output to the terminal.

class Greeting
{

    # Attributes.
    $message = "Hello"
    $name = ""

}

# Class instance.
$greeting = New-Object Greeting

# Set attribute.
$greeting.name = "Fred"

# Format the greeting and display it in the terminal.
Write-Host "$($greeting.message) $($greeting.name)!"

Adding Methods

Methods within a Class carry out the actions of an Object. In the previous example the message is formed by concatenating two Attributes together, separated by a space, with an exclamation mark on the end. This formatting of the message could be carried out in a Method. The advantage of doing this is that it could be reused any number of times and if the format of the message needs changing it only needs to be altered in one place. In the ‘return’ statement, ‘$this’ is used to reference the current object when referring to attributes from within. The difference between referencing an Attribute and a Method is that with a Method the name is followed by ‘()’, as with ‘DisplayGreeting()’ below. One final item to note is the data type that is specified before the method name. This is the data type of the value to be returned.

class Greeting
{

    # Attributes.
    $message = "Hello"
    $name = ""

    # Method.
    [string] DisplayGreeting()
    {

        return "$($this.message) $($this.name)!"

    }

}

# Class instance.
$greeting = New-Object Greeting

# Set attribute.
$greeting.name = "Fred"

# Display greeting.
Write-Host $greeting.DisplayGreeting()

Adding a Constructor

A constructor is a special type of method that is called automatically when a Class is instantiated. The name of the constructor is the same as the Class name and there is no return type or return statement included within this method.

class Greeting
{

    # Attributes.
    [string] $message
    [string] $name

    # Constructor.
    Greeting([string] $pMessage, [string] $pName)
    {

        $this.message = $pMessage
        $this.name = $pName

    }

    # Method.
    [string] DisplayGreeting()
    {

        return "$($this.message) $($this.name)!"

    }

}

# Class instance with parameters.
$greeting = New-Object Greeting("Hello", "Fred")

# Display greeting.
Write-Host $greeting.DisplayGreeting()

In the above example the attributes are defined in the Class but not set. A constructor is included that accepts two string parameters. Within the constructor, the attributes are set using the parameter values passed in. The 'DisplayGreeting' method is called as before.