I was recently asked for help with how to make a “class” in PowerShell. In the training session, we looked at:
Properties
Entered the typical Properties that a human might have.
Methods
Define the actions that the Human class, can perform.
Data Verification
Ensure that all the required properties are entered when the object is created.
When initiated our Class was going to create a “Human” object with many of the properties a human might have. Here is the Class in its entirety.
Class Code + Examples using the Object.
class Human {
[string]$FirstName
[string]$LastName
[int]$BirthMonth
[int]$BirthDay
[int]$BirthYear
[int]$HeightFeet
[int]$HeightInches
[string]$Country
Human([string]$FirstName,[string]$LastName,[int]$BirthMonth,[int]$BirthDay,[int]$BirthYear,[int]$HeightFeet,[int]$HeightInches){
$this.FirstName = $FirstName
$this.LastName = $LastName
$this.BirthMonth = $BirthMonth
$this.BirthDay = $BirthDay
$this.BirthYear = $BirthYear
$this.HeightFeet = $HeightFeet
$this.HeightInches = $HeightInches
}
GetAge(){
$CurrentDate = Get-Date
$BirthDate = Get-Date -Year $this.BirthYear -Month $this.BirthMonth -Day $this.BirthDay
$Age = [Math]::Truncate(($CurrentDate - $BirthDate).TotalDays / 365)
Write-Host $Age
}
[void]IncreaseAge($Age){
$this.BirthYear = $this.BirthYear - $Age
}
}
$Human = New-Object Human Homer,Simpson,01,30,1982,5,10
$Human
$Human.Country = "Canada"
$Human
Write-Host
$Age = $Human.GetAge()
$Age
$Human.IncreaseAge(5)
$Human.GetAge()
Line 1 names the object.
class Human {
2 through 9 define the properties of the object
[string]$FirstName
[string]$LastName
[int]$BirthMonth
[int]$BirthDay
[int]$BirthYear
[int]$HeightFeet
[int]$HeightInches
[string]$Country
After I wrote the code, the person asked why I had written this section and what does it do.
Human([string]$FirstName,[string]$LastName,[int]$BirthMonth,[int]$BirthDay,[int]$BirthYear,[int]$HeightFeet,[int]$HeightInches){
$this.FirstName = $FirstName
$this.LastName = $LastName
$this.BirthMonth = $BirthMonth
$this.BirthDay = $BirthDay
$this.BirthYear = $BirthYear
$this.HeightFeet = $HeightFeet
$this.HeightInches = $HeightInches
}
I let them know adding these lines of code allows the items listed to be applied directly to the Object when it is created. The items listed are now also required while creating the Object.
If you don’t include all the items in the list, you will get an error that a constructor hasn’t been found.
Not including “Country” in the code, ensures it isn’t a required Property of the Object.
The two items puzzled my trainee. They didn’t know why the method was set to [void] and the other wasn’t. I explained if your Method doesn’t return output, it needs to be created with a [void] type.
GetAge(){
$CurrentDate = Get-Date
$BirthDate = Get-Date -Year $this.BirthYear -Month $this.BirthMonth -Day $this.BirthDay
$Age = [Math]::Truncate(($CurrentDate - $BirthDate).TotalDays / 365)
Write-Host $Age
}
[void]IncreaseAge($Age){
$this.BirthYear = $this.BirthYear - $Age
}
You can see GetAge returns an answer. IncreaseAge changes the data of one of the properties but doesn’t return the info. Because of this IncreaseAge HAS to have the [void] type set. GetAge doesn’t need it.
Code - Creating the Object’s Class.
$Human = New-Object Human Homer,Simpson,01,30,1982,5,10
$Human
The output looked like this:
FirstName : Homer
LastName : Simpson
BirthMonth : 1
BirthDay : 30
BirthYear : 1982
HeightFeet : 5
HeightInches : 10
Country :
Code - Entering the Country Name
As you can see there are no errors and there is no country set. Next up, we are going to add the country name.
$Human.Country = "Canada"
$Human
The script will then show the Country Property has been set.
FirstName : Homer
LastName : Simpson
BirthMonth : 1
BirthDay : 30
BirthYear : 1982
HeightFeet : 5
HeightInches : 10
Country : Canada
We then want to figure out how old Homer Simpson is:
$Age = $Human.GetAge()
$Age
The age of Homer Simpson is:
42
The next bit of code tells the object to increase the age of the Human Object.
$Human.IncreaseAge(5)
$Human.GetAge()
As you would expect you can see that it outputs the new age by 5 since we increased it by that amount.
47
What might not be as apparent is how I increased the age of the Human Object? The object’s birth date year was reduced by the number entered. So the year was 1982 and after selecting to Increase Age by 5, the new birth year date is 1977.
FirstName : Homer
LastName : Simpson
BirthMonth : 1
BirthDay : 30
BirthYear : 1977
HeightFeet : 5
HeightInches : 10
Country : Canada
I hope this post gives you everything you might need to think about when creating your first Class.