I have an array of objects:
$people= @()
foreach ($person in $databaseOfGuests)
{
$people += @{ "FirstName"=$person.FirstName; "LastName"=$person.LastName }
}
Now I want to remove duplicates from $people, is it possible to do this in PowerShell? Something like:
$people = $people | Select -Uniq
I have two arrays $people1 and $people2, I need to get array of all people that are in $people1 but not in the $people2 and vise versa. Something like:
$peopleInPeople1ButNotInPeople2 = $people1.Substruct($people2)
$peopleInPeople2ButNotInPeople1 = $people2.Substruct($people1)
Is it possible to do it in one line in PS?
$people += $personinstead. Then you can compare using-notinoperator+=is inefficient, because a new array must be created behind the scenes in every iteration, given that arrays are of fixed size; a much more efficient approach is to use aforeachloop as an expression and let PowerShell itself collect the outputs in an array:[array] $outputs = foreach (...) { ... }- see this answer. In case you need to create arrays manually, e.g. to create multiple ones, use an efficiently extensible list type - see here.