How can I prevent repeating array entries in Powershell

Hello Powershell Experts,

I have two test groups as follows
TestGroup1 – TestGroup1 is a member of: Sales, Marketing, SouthSales and WestSales
TestGroup2 – TestGroup2 is a member of: Sales, Marketing, NorthSales and EastSales

I have the following code to populate an array (note I’m using the quest AD commandlets):
$MasterGroup = @(Get-QADGroup -ContainsMember testGroup1 | select samaccountname)
$MasterGroup +=@(get-qadgroup -ContainsMember testGroup2 | select samaccountname)

 
After running the code above, when I check the Value of $MasterGroup, I get the following:

SamAccountName
-------------------------
Sales
Marketing
SouthSales
WestSales
Sales
Marketing
NorthSales
EastSales

I understand why this is happening, but its not what I’m looking for.  I want the value of $MasterGroup to be:
SamAccountName
-------------------------
Sales
Marketing
SouthSales
WestSales
NorthSales
EastSales

How would I do a check so that entries into the array are not repeated?

Thanks,
Nick
LVL 1
ndalmolin_13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
You could just add the select -Unique at the end:

$MasterGroup = @(Get-QADGroup -ContainsMember testGroup1 | select samaccountname)
$MasterGroup +=@(get-qadgroup -ContainsMember testGroup2 | select samaccountname)
$MasterGroup = $MasterGroup | select -Unique

Open in new window

ndalmolin_13Author Commented:
When I do the select-unique, my output is:
SamAccountName
 -------------------------
 Sales
ndalmolin_13Author Commented:
This is what I'm trying right now:
$Role1 = @(Get-QADGroup -ContainsMember testGroup1 | select samaccountname)
$Role2 = @(get-qadgroup -ContainsMember testGroup2 | select samaccountname)
foreach ($item in $Role2)
{
    if($Role1 -notcontains $item.samaccountname)
    {
    $Role1 += $item.samaccountname
    }

}
Virus Depot: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. View our webinar recording to learn how to best defend against these attacks!

Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Oops, those are objects and not strings.

This should do it:
$MasterGroup = @(Get-QADGroup -ContainsMember testGroup1 | select samaccountname)
$MasterGroup +=@(get-qadgroup -ContainsMember testGroup2 | select samaccountname)
$MasterGroup = $MasterGroup.samAccountName | select -Unique

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndalmolin_13Author Commented:
That is too cool.  Simple and to the point.  Thanks for your help.
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Glad to help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.