2016-05-12 23 views
3

我想帮助比较两个数组的用户,并抛出两个数组中存在或匹配的用户,然后将结果扔到最终数组中。例如:在比较数组时,删除匹配项

###define arrays 
$array1 = @("bill","eric","james","sarah") 
$array2 = @("bill","scott","sarah","nancy") 

###Combine/Filter? arrays and remove users that exist in both arrays 
$result = ($array1 + $array2 | some fancy match removal goes here) 
$result 
eric,james,scott,nancy 

我想确保匹配从两个数组合并时完全删除。所以如果两个阵列中都存在“sarah”,我想把她完全从最终结果中删除。那可能吗?

回答

3

使用Compare-Object以提取在两个源阵列是独特的元素:

$result = Compare-Object $array1 $array2 | Select-Object -Expand InputObject 
+1

这是太棒了 - 非常感谢你的帮助 – Intrepdmind