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 = 

$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:

1 Like