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?
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

0
ndalmolin_13Author Commented:
When I do the select-unique, my output is:
SamAccountName
 -------------------------
 Sales
0
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
    }

}
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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

0

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.
0
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Glad to help!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.