How can I remove the items in $b from $a?
$a = "aa","bb","cc","dd","ee"
$b = "bb","dd"
Do you know a good solution?
Thanks!
How can I remove the items in $b from $a?
$a = "aa","bb","cc","dd","ee"
$b = "bb","dd"
Do you know a good solution?
Thanks!
The Compare-Object command is the easiest way of doing this, although it's not very robust, if this is for use in production you may want to roll a more thought-out solution.
$c = Compare-Object -ReferenceObject $a -DifferenceObject $b | ? SideIndicator -eq '<=' | Select -Expand InputObject
Another way which is quite similar but more readable in my opinion is:
$c = $a | ? {$_ -notin $b}
Another way would be using the Group-Object command, and selecting unique entries, but this will include ones which were in $b but NOT in $a.
$c = $a+$b | Group-Object | ? Count -eq 1 | Select -Expand Group
all of these methods will output:
aa
cc
ee