Skip to content

Instantly share code, notes, and snippets.

@Akaitatsu
Last active January 19, 2018 14:46
Show Gist options
  • Save Akaitatsu/5f518b8c29b2f2e1f410a35b7d7f0dea to your computer and use it in GitHub Desktop.
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
# 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