-
-
Save am1ru1/2921862c1d1e8000e21f4820a6a7da2f to your computer and use it in GitHub Desktop.
PowerShell Deepcopy demo
This file contains 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
# Get original data | |
$data = @{ | |
First = 'Justin'; | |
Last = 'Dearing'; | |
Resume = @{ | |
Experience = [HashTable[]]@( | |
@{ | |
StartDate = [DateTime] (Get-Date '2002-03-23'); | |
EndDate = [DateTime] (Get-Date '2007-05-15'); | |
Company = 'ACME ISP'; | |
Title = 'Support Technician'; | |
}; | |
@{ | |
StartDate = [DateTime] (Get-Date '2007-05-16'); | |
EndDate = [DateTime] (Get-Date) | |
Company = 'Innitech'; | |
Title = 'Developer'; | |
}; | |
) | |
}; | |
} | |
Write-Host | |
Write-Host $data.GetType().FullName | |
Write-Host "Shallow Copy" | |
$dataShallow = $data.Clone(); | |
$dataShallow.Resume.Experience[1].Title = 'Senior Developer'; | |
$data.Resume.Experience |%{ New-Object PSObject -Property $_ } | |
# Serialize and Deserialize data using BinaryFormatter | |
$ms = New-Object System.IO.MemoryStream | |
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter | |
$bf.Serialize($ms, $data) | |
$ms.Position = 0 | |
$dataDeep = $bf.Deserialize($ms) | |
$ms.Close() | |
$dataDeep.Resume.Experience[1].Title = 'Junior Developer'; | |
Write-Host "Deep Copy Original" | |
$data.Resume.Experience |%{ New-Object PSObject -Property $_ } | |
Write-Host "Deep Copy" | |
$dataDeep.Resume.Experience |%{ New-Object PSObject -Property $_ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment