Class & Enum

You can define Class and Enum.

Class

Class syntax can be used, PowerShell V5 or later.
Can use Class syntax, Enum declarations, properties, methods, inheritance, etc.

Define the class.

Define the class as follows.

PS> class Country {
    [string]$name
    [string]$language
}

Create an instance.

You can create an instance with New-Object CLASS-NAME.
Since v5, you can also create an instance with [CLASS-NAME]::new(). Recommended for v5 and later.

PS> New-Object Country

name language
---- --------

PS> [Country]::new()

name language
---- --------

Set initial values for properties.

PS> class Country {
>>     [string]$name="NAME"
>>     [string]$language="LANGUAGE"
>> }
PS> [Country]::new()

name language
---- --------
NAME LANGUAGE

Create an instance by specifying the value of a property.

PS> [Country]@{name="AAA"; language="BBB"}

name language
---- --------
AAA  BBB

static & hidden

PS> class mynum {
>> static $number1 = 100        # static
>> hidden static $number2 = 200 # hidden
>> }
PS> [mynum]::number1
100
PS> [mynum]::number2
200
PS> [mynum] | Get-Member -Type Property -Static

   TypeName: mynum

Name    MemberType Definition
----    ---------- ----------
number1 Property   static System.Object number1 {get;set;}

PS> [mynum] | Get-Member -Type Property -Static -Force

   TypeName: mynum

Name    MemberType Definition
----    ---------- ----------
number1 Property   static System.Object number1 {get;set;}
number2 Property   static System.Object number2 {get;set;}

Constructor

class Country {
    [string]$name
    [string]$language
 
    # Constructor
    Country([string]$name, [string]$language) {
        $this.name = $name
        $this.language = $language
    }
}
PS> $country = [Country]::new('Japan','Japanese')
PS> $country
name  language
----  --------
Japan Japanese

Multiple constructors

PS> class Country {
>>     [string]$name
>>     [string]$language
>>
>>     Country() {
>>     }
>>
>>     Country([string]$name, [string]$language) {
>>         $this.name = $name
>>         $this.language = $language
>>     }
>> }
PS> $country = [Country]::new()
PS> $japan = [Country]::new('Japan','Japanese')
PS> $country
name language
---- --------

PS> $japan
name  language
----  --------
Japan Japanese

Methods

PS /Users/miajimyu/Desktop> class Country {
>>     [string]$name
>>     [string]$language
>>  
>>     [void] GetProperty() {
>>         Write-Host "name`t`t:" $this.name
>>         Write-Host "language`t:" $this.language
>>     }
>> }
PS /Users/miajimyu/Desktop> $country = [Country]::new()
PS /Users/miajimyu/Desktop> $country.GetProperty()
name            : 
language	: 
PS /Users/miajimyu/Desktop> $country.name = 'japan'
PS /Users/miajimyu/Desktop> $country.language = 'japanese'
PS /Users/miajimyu/Desktop> $country.GetProperty()
name    	: japan
language        : japanese

Example

# Country.ps1
class Country {
    [string]$name
    [string]$language
 
    Country() {
    }

    Country([string]$name, [string]$language) {
        $this.name = $name        
        $this.language = $language       
    }

    [void] GetProperty() {
        Write-Host "name`t`t:" $this.name
        Write-Host "language`t:" $this.language
    }
}
 
class USA : Country {
    USA() {
        $this.name = "United States of America"
        $this.language = "English"
    }
}
 
class Japan : Country {
    Japan() {
        $this.name = "Japan"
        $this.language = "Japanese"
    }

    [void] GetProperty() {
        Write-Host "名前`t`t:" $this.name
        Write-Host "言語`t`t:" $this.language
    }
}
 
function Main {
    $usa = [USA]::new()
    $usa.GetProperty()
    $japan = [Japan]::new()
    $japan.GetProperty()
}

Main
PS> .\Country.ps1
name            : United States of America
language        : English
名前            : Japan
言語            : Japanese

Enum

How to define an enum.

enum Colors {
    Red
    Blue = 10 # You can also set the value
    Green
}

How to access and check the values.

# How to access the values
PS> [Colors]::Red
Red
PS> [Colors] 0   
Red
PS> [Colors] "Red"
Red

# How to check the values
PS> [Colors]::Red.value__
0
PS> [int][Colors]::Red     # You can see the value by casting it with [int].
0
PS> [Colors]::Blue.value__
10
PS> [Colors]::Green.value__
11

Example of combining an Enum with a switch statement.

Example1

# GetRGB.ps1
enum Colors {
    Red
    Blue
    Green
}

foreach ($item in $args) {
    switch ($item) {
        Red     {"R"}
        Blue    {"B"}
        Green   {"G"}
        Default {"Unknown"}
    }
}

Execution result

PS> .\GetRGB.ps1 green red blue pink
G
R
B
Unknown

Example2

If you specify [$Colors], you will get a runtime error.

# GetRGB.ps1
enum Colors {
    Red
    Blue
    Green
}

foreach ($item in $args) {
    switch ([Colors]$item) {    # Add [Colors]
        Red     {"R"}
        Blue    {"B"}
        Green   {"G"}
        Default {"Unknown"}
    }
}

Execution result

PS /Users/user/Desktop> .\GetRGB.ps1 green red blue pink
InvalidArgument: /Users/user/Desktop/GetRGB.ps1:7:9
Line |
   7 |  switch ([Colors]$args) {
     |          ~~~~~~~~~~~~~
     | Cannot convert value "green,red,blue,pink" to type "Colors". Error: "Unable to match the identifier name green,red,blue,pink to a valid
     | enumerator name. Specify one of the following enumerator names and try again: Red, Blue, Green"

flags()

If you add [flags()], you can also calculate bits.

PS> [flags()] enum mybit {zero = 0x01; one = 0x02; two = 0x04; three = 0x08}
PS> [int][mybit] "one,two"
6
PS> [int][mybit] "zero,one,two,three"
15

YouTube

Click here for a video explanation.

About Classes | Microsoft Docs

About Enum | Microsoft Docs