A class is a user-defined type that can be created using the keyword “Class”. PowerShell is an object-oriented language. This means when the users get an output in the PowerShell console after running commands, objects are returned. The developers instantiate the objects with classes. PowerShell is then used to represent the schemas or definitions of the objects.
This post will elaborate on the PowerShell classes in detail.
PowerShell Classes: Getting Started
These methods will be overviewed while explaining PowerShell classes:
Method 1: Creating a Class
The class creation needs a “Class” keyword to initiate a class. The example is provided below to demonstrate the process of creation.
Example
Check out the given code:
[string]$Name
[int]$Age
[string]$Profession
}
$emp = [Employee]::new()
$emp.Name = "John Doe"
$emp.Age = 24
$emp.Profession = "Doctor"
$emp
According to the above code:
-
- First, declare a class named “Employee”.
- Within the class, specify the stated variables.
- After that, create an object of the class named “emp”.
- Now, assign the values to the specified class variables.
- Lately, invoke the allocated object “emp”:
The value of the created “$emp” object has been returned to the PowerShell console.
Method 2: Class Constructors
The class constructors have the same name as the class name. More specifically, constructors are used to define the default values and help to validate the logic at the time of creating new objects.
Example
This example will demonstrate how to create constructors in a class:
[string]$Name
[int]$Age
[string]$Profession
Employee() {
$this.Name = "John Philips"
$this.Age = 24
$this.Profession = "Doctor"
}
Employee([string]$Name, [int]$Age, [string]$Profession) {
$this.Name = $Name;
$this.Age = $Age;
$this.Profession = $Profession;
}
}
$emp1 = [Employee]::New()
$emp2 = New-Object Employee James, 26, "Cook"
$emp1
$emp2
In the stated code above:
-
- First, recall the discussed approaches for declaring a class and specifying the variables.
- After that, create a class constructor and assign the stated values to the specified variables via “this”.
- Now, create a parameterized constructor, having the parameters identical to the specified ones.
- In its definition, assign the specified variables the passed values.
- Create two objects of the class named “emp1”, and “emp2”, respectively.
- In the latter object, pass the stated values to the parameterized constructor.
- Lastly, access the two objects “emp1”, and “emp2”, respectively:
Method 3: Creating Methods
A method is simply a set of instructions that is used to specify the actions that can be performed on an object.
Example
This illustration will demonstrate to create a method in the class:
[int]$Height
[int]$Age
[void]Grow() {
$heightIncrease = Get-Random -Min 1 -Max 5;
$this.Height += $heightIncrease;
$this.Age += 1
}
}
$emp = [Employee]::New()
for ($i = 0; $i -lt 5; $i++) {
$emp.Grow()
$emp
}
In this code:
-
- Define a class named “Employee”.
- Within the class, specify the stated variables of the “int” type.
- Now, define a function named “Grow()”.
- In the function definition, generate the random values within the specified range and store it to a variable, i.e., “heightIncrease”.
- In the next step, refer to the specified height via “this” and increment it based on the generated random value.
- Likewise, refer to the “Age” and increment it by “1”.
- After that, create a class object named “emp” and apply a “for” loop to generate the random height and age “5” times within the associated function, i.e., “Grow()”:
Method 4: Class Inheritance
The class inheritance is the process to use the one to create another class, making it the base of another class. The inheriting class is called the parent class or subclass. Moreover, the inheritance of one class from more than two classes is called the “super” class.
Example
The following example will demonstrate the concept of the class inheritance:
[string]$Name
[int]$Age
[string]$Profession
Employee() {
$this.Name = "John Doe";
$this.Age = 24
$this.Profession = "Doctor"
}
}
class Desig : Employee {
[string]$Designation = "Senior"
}
$emp = [Desig]::new()
$emp
According to the above code snippet:
-
- First, define the class named “Employee”.
- Within the class, specify the variables with type “int” and “string”.
- Now, define a class constructor.
- The class constructor is referring to the class values using the “this”.
- After that, create a child class “Desig” inherited from the parent class “Employee” to add another variable “string” type value.
- Then, create an object named “emp” and assign the child class “[Desig]::new()” to it:
As you can see, the object of the child class has successfully inherited the values from the parent class constructor.
Conclusion
PowerShell classes are the representation of the schemas or definitions of objects. As PowerShell is an object-oriented programming language, objects are instantiated using PowerShell classes. This post has covered details about classes including methods, constructors, or inheritance.