Thursday, July 27, 2023

Merge Hashtables in Powershell


We can merge hashtables and perform some operations on merged hashtable  

```sh

#Function is used to merge hashtables.

function Merge-Hashtables([ScriptBlock]$Operator) {
  $Output = @{}
    ForEach ($hashtable in $input) {
        If ($Hashtable -is [Hashtable]) {
            ForEach ($key in $hashtable.Keys) {$Output.$key = If ($Output.ContainsKey($key)) {@($Output.$key) + $hashtable.$key} Else  {$hashtable.$key}}
        }
    }
    If ($Operator) {ForEach ($Key in @($Output.Keys)) {$_ = @($Output.$Key); $Output.$Key = Invoke-Command $Operator}}
    $Output
}



# Usage 


$hash1 = @{ Number = 1; Shape = "Rectangle"; Color = "Blue"}
$hash2 = @{ Number = 2; Shape = "Square"; Color ="Red"}
$hash3 = @{ Number = 3; Shape = "Circle"; Color ="Green"}

```

### Example-1

``` sh
$hash1,$hash2,$hash3 |  Merge-Hashtables

#Result 
  Name                           Value                                                                                                                   
  Color                          {Blue, Red, Green}
  Shape                          {Rectangle, Square, Circle}
  Number                         {1, 2, 3}
   
```
### Example-2

``` sh
$hash1,$hash2,$hash3 |Merge-Hashtables  {$_ -join ","}

#Result 

 Name                           Value 
 ----                           -----
 Color                          Blue,Red,Green
 Shape                          Rectangle,Square,Circle
 Number                         1,2,3                                                                                                                     
```

### Example-3

```sh
$hash1,$hash2,$hash3 |  Merge-Hashtables {$_ | Sort-Object}

#Result 
 
 Name                           Value                                                                                                                     
 ----                           -----                                                                                                                     
 Color                          {Blue, Green, Red}                                                                                                       
 Shape                          {Circle, Rectangle, Square}                                                                                               
 Number                         {1, 2, 3}       

```
### Example-4

```sh

$hash1,$hash2,$hash3 |  Merge-Hashtables {$_ | Select -First 1}

#Result 
 
  Name                           Value 
  ----                           -----
  Color                          Blue
  Shape                          Rectangle
  Number                         1        
  
```

### Example-5

```sh
$hash1,$hash2,$hash3 |  Merge-Hashtables { $_ | Select -First 2}

#Result 

 Name                           Value                                                                                                                   
 ----                           ----- 
 Color                          {Blue, Red}
 Shape                          {Rectangle, Square}
 Number                         {1, 2}     
 
```

No comments:

Post a Comment