Powershell Basics: Writing Classes

As of PowerShell 5.0 you can create your own classes. A class is a template or definition of an object. Conversely an object is an instance of a class. Classes can contain methods, properties, enums, and other things.

#Define the Class
Class RandomBattle {
   #Define Properties of this class
   [String]$Terrain
   [Int]$EnemyCount
   #Using the static keyword makes this property always available
   static [Int]$PartyMemberCount = 3
}

#Instantiate a New Object
$Battle = New-Object RandomBattle
$Battle.EnemyCount = 5

#Access the static Property we assigned
[RandomBattle]::PartyMemberCount

 

Adding Enums

Using Enums is a good way of validating the data going in by restricting it to an enumerated set of values.

Class RandomBattle {
   [String]$Terrain
   [Int]$EnemyCount
   static [Int]$PartyMemberCount = 3
   #Add the defined Enum as a property in your class
   [PartyMembers]$NextTurn
}

#Define the Enum
Enum PartyMembers {
   Cloud = 1
   Tifa = 2
   Barret = 3
}

#Here is the Enum working on its own
[PartyMembers]::Cloud
[PartyMembers]::Cloud.value__
#Here we Instantiate a New Object and set the NextTurn Property which is an Enum
$Battle = [RandomBattle]::New()
$Battle.NextTurn = 1

 

Adding Methods

You can also add methods to your class. Note here that we refer to the object that called the method as $this in the method definition. You can write methods without parameters by simply using ()

Class RandomBattle {
   [String]$Terrain
   [Int]$EnemyCount
   static [Int]$PartyMemberCount = 3
   EnemyKO($count) {$this.EnemyCount -= $count }
}

$Battle = [RandomBattle]::New()
$Battle.EnemyCount = 5
$Battle.EnemyKO(2)

 

Throwing Errors

Instead of Write-Error or PSCmdlet.ThrowTerminatingError, in Classes you should use the Throw Key word.

Class RandomBattle {
   EnemyKO() {
      Try {
         #Some failing command
      }
      Catch {
         Throw $_
         Throw "Custom Error Message"
      } 
   }
}

Inheriting Classes

It is important to design your classes well. For that reason class inheritance is often important. If you can identify properties,methods, etc that are common to different classes you will be working with you can inherit these from a parent class

#Define Class with Common Properties
Class Battle{
   [String]$Terrain
   static [Int]$PartyMemberCount = 3
}
#Define Class with more specific properties. The ':' means inherit
Class RandomBattle : Battle {
   [Int]$EnemyCount
}
Class BossBattle : Battle {
   PlayCinematic() {
      Write-Host "Aeries no!"
   }
}

$Rand = [RandomBattle]::new()
$boss = [BossBattle]::new()

 

The Using Statement

This new Powershell 5.0 Keyword allows you to import other powershell classes from modules and to also import namespaces allowing you to reference .NET classes by there name only


Using Namespace System.Xml.Linq
Using Module MyClass

#Instead of the long [System.Xml.Linq.XDocument]::new() we can just use:
$XDoc = [XDocument]::new()

#Becuase we are Using the MyClass class we can instantiate MyClass:
[MyClass]::new()

Leave a Reply

Your email address will not be published. Required fields are marked *