Merging hashtables in PowerShell: how?

前端 未结 12 1907
猫巷女王i
猫巷女王i 2021-01-07 18:26

I am trying to merge two hashtables, overwriting key-value pairs in the first if the same key exists in the second.

To do this I wrote this function which first remo

12条回答
  •  醉梦人生
    2021-01-07 19:16

    Here is a function version that doesn't use the pipeline (not that the pipeline is bad, just another way to do it). It also returns a merged hashtable and leaves the original unchanged.

    function MergeHashtable($a, $b)
    {
        foreach ($k in $b.keys)
        {
            if ($a.containskey($k))
            {
                $a.remove($k)
            }
        }
    
        return $a + $b
    }
    

提交回复
热议问题