Create Class for Object

PHOTO EMBED

Mon Nov 21 2022 22:58:03 GMT+0000 (Coordinated Universal Time)

Saved by @Onibus #powershell

[pscustomobject]@{ Name = 'bob'; Id = 20 }
# can be
[Employee]@{ Name = 'bob'; Id = 20 }

# Create class that we can populate
class Employee {    
    [ValidateNotNullOrEmpty()]
    [string]$Name    
    
    [ValidateRange(0,40)]
    [int]$Id
}

[Employee]@{ Name = 'bob'; Id = 20 }
[Employee]@{ Name = 'Jen'; Id = 33 }

# OR add object to a list

$list = [system.collections.generic.List[object]]::new()
class Person {
    $name
    $location
    $age
}

$Person = [Person]@{
    name = "Noah"
    location = "here"
    age = "old enough"
}

$list.add($person)
content_copyCOPY