Posted On October 26, 2020

PowerShell: Convert Array to Hash and back to Array

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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++ ) {
              try{$hash.Add($arr[$i][0],$arr[$i][1])}catch{}
            }
            return $hash
        }
     
    function hashToArray($hash){
        $arr=@();
        [string[]] $arr = ($hash | Out-String -Stream) -ne '' | select -Skip 2
        return $arr
        }

Leave a Reply

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

Related Post

Office 365 Active Directory Hybrid Accounts Administration

Intro This scenario assumes that Azure AD Sync has been used to synchronize on premise…

PowerShell: Install Visual Studio 2019 Community Version

# Install Chocolateyif (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}#…

PowerShell: Set ACL

# setAcl-v0.01.ps1# # What this script does:# 1. Set permissions on a set of "destination…