Merging hashtables in PowerShell: how?

前端 未结 12 1981
猫巷女王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:13

    I think the most compact code to merge (without overwriting existing keys) would be this:

    function Merge-Hashtables($htold, $htnew)
    {
       $htnew.keys | where {$_ -notin $htold.keys} | foreach {$htold[$_] = $htnew[$_]}
    }
    

    I borrowed it from Union and Intersection of Hashtables in PowerShell

提交回复
热议问题