How to Define Classes in PowerShell

05. September 2016 PowerShell 0

PowerShell 5 comes with a new capability to define classes in the same way we define in C#. In this tutorial we are going to see how to define a simple class with constructor, properties, and methods.

Please check the Windows PoweShell Tutorial to learn the PowerShell basics.

Development Tool

In this tutorial I am going to use Visual Studio 2015 to edit my .ps1, .psm1 files. You might wonder why not the default ISE? Visual Studio is a great development tool which also provides the ability to edit PowerShell scripts. It gives you all the debugging tools available in ISE along with better editing and coding experience.

Visual Studio also gives you and interactive window which is similar to the PowerShell console.

Create a new PowerShell project in Visual Studio

Create a new PowerShell Script Project from Installed, Templates, Other Languages, PowerShell.

Define a class

In this tutorial we are going to create a simple class which calculates the area of a circle. We are going to add a property called radius. We are going to add a method called Area which returns the area of the circle. We will also be having a static method called CalculateArea to be used without initializing the class.

Right click on the project and add a new PowerShell Script Module called Circle. We are going to define our class definition in this module.

Edit the file as below:

# Define a class called Circle
Class Circle {
	
	# default constructor
	Circle() {

	}

	# constructor to accept the radius
	Circle([double] $radius) {
		# setting the class Radius property using 'this' keyword
		$this.Radius = $radius
	}

	# define properties
	[double] $Radius = 0
	[double] $PI = 3.14

	# define a class method returning the area
	[double] Area() {
		$area = $this.Radius * $this.PI

		return $area
	}

	# define an overload method to accept radius
	[double] Area([double]$radius) {
		$area = $radius * $this.PI

		return $area
	}

	# define a static method
	static [double] CalculateArea([double]$radius) {
		$area = $radius * 3.14

		return $area
	}
}

Open Script.ps1 from the solution and edit it as below (if the file doesn’t exist right click on the project and add a new PowerShell Script item):

# import the circle module
# To use a module, you first need to import it
# you need to add -Force if you have loaded the module an dedited it farther
using module ".\Circle.psm1"

# create an instance of the circle class using default constructor
$circle = [Circle]::new()

# edit the raduis property
$circle.Radius = 5

# displays area
Write-Host "The area of the circle is: $($circle.Area())"

# create an instance of the circle class using alternate constructor
$circle2 = [Circle]::new(10)
Write-Host "The area of the circle is: $($circle2.Area())"

# calling Area overloaded method
Write-Host "The area of the circle is: $($circle2.Area(20))"

# using static method without instanciating any object
Write-Host "The area of the circle is: $([Circle]::CalculateArea(30))"

Output:


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.