Merging two Variables (containing AD Group Names)

I have code that pulls AD groups from several locations. One piece that pulls codes from three different OUs; and another section that pulls a list of AD groups from a text file. I would like all groups to be in a single variable so that I can query group membership within a single foreach. How would that be done?

$OUs = 
"OU=Level1,OU=Level2,OU=Application,OU=Group,DC=DOmain,DC=NET"
"OU=Level3,OU=Level4,OU=Division,OU=Group,DC=Domain,DC=NET"
"OU=Level1,OU=Level2,OU=Department,OU=Group,DC=Domain,DC=NET"

$OUGroups = $OUs | foreach-object{(Get-ADGroup -Filter * -SearchBase $_).Name}

$FileGroups = Get-Content ".\groups.txt"

You can simply provide more than one variable delimitted with a comma …

$OUGroups, $FileGroups

Or you could combine them in a third variable …

$AllGroups = $OUGroups + $FileGroups

Or you could add the content of one variable to the other …

$OUGroups += $FileGroups

Here you have some more stuff to read about … you should read that completely including the examples to learn how it works! :point_up_2:t4:

2 Likes

I signed up just so I could give Olaf a hug for that solution. I was being stupid and trying to do it the hard way. $AllGroups = $Groups1 + $Groups2 works so easy I’m scolding myself!

Barry,
Welcome to the forum. :wave:t3:

Thanks. That’s very nice of you. :love_you_gesture:t3:

Yeah … sometimes you can’t see the forest for the trees. :man_shrugging:t3: