Last active
January 19, 2018 14:46
-
-
Save Akaitatsu/5f518b8c29b2f2e1f410a35b7d7f0dea to your computer and use it in GitHub Desktop.
Example of creating and using classes with constructors in PowerShell 5+ to reduce repetitive PSCustomObject code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Reference: https://blogs.technet.microsoft.com/heyscriptingguy/2015/09/09/powershell-5-classes-constructor-overloading/ | |
Class Dog { | |
[string] $Name; | |
[string] $Sex; | |
[string] $Color; | |
[string] $Breed; | |
[int] $BarkVolume; | |
Dog ( | |
[string] $name, | |
[string] $sex, | |
[string] $color, | |
[string] $breed, | |
[int] $barkVolume | |
) | |
{ | |
$this.Name = $name; | |
$this.Sex = $sex; | |
$this.Color = $color; | |
$this.Breed = $breed; | |
$this.BarkVolume = $barkVolume; | |
} | |
} | |
$doggos = @( ` | |
[Dog]::new("Zoe", "F", "Black and White", "Pitbull", 6), ` | |
[Dog]::new("Pete", "M", "White and Black", "Dorkmation", 4), ` | |
[Dog]::new("Heen", "M", "Light Brown", "Australian Shepherd", 5), ` | |
[Dog]::new("Pearl", "F", "Light Brown", "Hayound", 11) ` | |
) | |
$doggos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment