PowerShell: Convert Array to Hash and back to Array

    # convert 2D Array to Hash Table while removing any duplicate keys
    function arrayToHash($arr){
            $hash = new-object System.Collections.Hashtable
            $arr=$arr|sort -Descending # Higher (more recent) values on top
            for ( $i = 0; $i -lt $arr.Length; $i++ ) {
            return $hash
    function hashToArray($hash){
        [string[]] $arr = ($hash | Out-String -Stream) -ne '' | select -Skip 2
        return $arr
Posted on Categories Codes

Leave a Reply

Your email address will not be published. Required fields are marked *