public/Copy-WpcNode.ps1

function Copy-WpcNode {
    param([object] $Node)

    if ($Node -is [System.Collections.IDictionary]) {
        $copy = [ordered]@{}
        foreach ($key in $Node.Keys) {
            $copy[$key] = Copy-WpcNode $Node[$key]
        }
        return $copy
    }

    if ($Node -is [System.Collections.IEnumerable] -and $Node -isnot [string]) {
        $items = [System.Collections.Generic.List[object]]::new()
        foreach ($item in $Node) {
            $items.Add((Copy-WpcNode $item))
        }
        return $items.ToArray()
    }

    return $Node
}