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

Basic CSS: Adjust the Margin of an Element

<style>.injected-text {margin-bottom: -25px;text-align: center;}.box {border-style: solid;border-color: black;border-width: 5px;text-align: center;}.yellow-box {background-color: yellow;padding: 10px;}.red-box {background-color: crimson;color: #fff;padding:…

PowerShell: Get Terminal Service (Remote Desktop Service) Licenses Report

# rdsLicenseReport.ps1 $knownRdsComputers=$null # Options: (a) 'SERVER1','SERVER100' (b) Get-Content Path\To\RDServers.txt function getTsLicenses{ param( $knownRdsComputers, $domain=$env:userdnsdomain…

PowerShell: Trigger Azure AD Sync on Remote Server as a Domain Admin

# Trigger-AD-Sync.ps1# Requirements: https://blog.kimconnect.com/powershell-install-rsat/## This script does these things:# a. Obtain Domain Admin credential# b.…