Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This Gist provides a suggested pattern for serializing / deserializing PowerShell v5 class instances
class Person {
[string] $FirstName;
[string] $LastName;
[string] $Address;
Person([string] $First, [string] $Last) {
$this.FirstName = $First;
$this.LastName = $Last;
[string] Serialize() {
return $this | ConvertTo-Json;
static [Person] Deserialize([string] $Json) {
$Deserialized = ConvertFrom-Json -InputObject $Json;
$Person = [Person]::new($Deserialized.FirstName, $Deserialized.LastName);
$Person.Address = $Deserialized.Address;
return $Person;
### Instantiate the Person class
$Person = [Person]::new('Trevor', 'Sullivan');
$Person.Address = '111 First Street';
### Serialize the object instance
$JsonText = $Person.Serialize();
### Deserialize the object instance
$Person = [Person]::Deserialize($JsonText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment