I have a couple powershell scripts that do parts of what I need separately. But don't know how to combine into one script. I want to limit the result list to check ONLY the list of users I provide.
So I want to know -- Is User1 in both Group1 AND Group2? Is User2 in both Group1 and Group2? Is User3 in both Group1 and Group3? etc..
Here are my separate scripts. I can also run the first once, then get a new list, and run it against group two. But would like to have one script if possible.
First script checks list and returns list included in group using a Get-Content command and ForEach.
$users = Get-Content C:\temp\user_list.txt
$group = "group1"
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty samaccountname
ForEach ($user in $users) {
If ($members -contains $user) {
Write-Host "$user"
}
}
Second gets list of members from one Group1, then compares each user to Group2, returning positives.
#checks two specified groups against each other and returns who is in both lists
# Get the members of Group1
$group1 = get-adgroupmember "group1";
# Get the members of Group2
$group2 = get-adgroupmember "group2";
# Loop through each user in Group1
foreach ($group1user in $group1){
# For each user in Group1, loop through every user in Group2 looking for a match
foreach ($group2user in $group2){
# If a match is found
if ($group1user.samaccountname -eq $group2user.samaccountname) {
write-host $group1user.samaccountname;
}
}
}