0

I would like to write a script that searches for users and if those users are not members of both Group A and B then they get added to Group B. I have found a lot of conditionals for adding a member to one group if it does not exist in another, but not two groups.

Wasif
  • 14,755
  • 3
  • 14
  • 34
farkus5
  • 1
  • 1
  • 1
  • 1
    Not sure what your question is but you need to post your code to this site to get help with it. You wont find someone here who would write the code for you – Jawad Mar 11 '20 at 15:38

2 Answers2

1

You can just join two condition using -and operator:

get-aduser -filter * -searchbase "dc=domain,dc=local" | where-object {((get-aduser $_.samaccountname -properties memberof).memberof -ne "A") -and ((get-aduser $_.samaccountname -properties memberof).memberof -ne "A")} | ForEach {add-adgroupmember -identity "B" -member $_.samaccountname}
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

You've identified your use case/goals.

This a common task.

What you are asking, os covered in the PowerShell help files and the examples for the cmdlets: Get-ADGroupMember (activedirectory) | Microsoft Docs, Add-ADGroupMember - learn.microsoft.com,

... this is a simple if/then and you can see plenty of examples of this use case online. Search for it on the web and Youtube, 'Powershell AD group management'

Your question is really a duplicate of these stackoverflow Q&A's

Check if a group exists in AD using PowerShell

Check if the user is a member of a list of AD groups

postanote
  • 15,138
  • 2
  • 14
  • 25