SOLVED

PS Script | Inserting users in groups based on user fields content

Brass Contributor

Hi,

 

I am trying to enroll users in specific groups based on their department and office fields content. I can go to each group (via Entra) and define dynamic rules but it will take ages as I have hundreds of groups.

 

Is there a way, through scripting, to insert users in specific groups based on the content of user fields?

 

Example: userA with department=dpto1 and office=ofc1 would be automatically enrolled in group

dpto1-ofc1@domain

 

Thanks/Brgds

joao

1 Reply
best response confirmed by Joao Casqueiro (Brass Contributor)
Solution
Sure, should be doable via PowerShell, try something like this:

foreach ($user in $users) {
Add-DistributionGroupMember ($user.department + $user.office) -Member $user.UserPrincipalName
}

where I'm assuming you're passing a user object (Get-User), so the aforementioned properties are readily available.
1 best response

Accepted Solutions
best response confirmed by Joao Casqueiro (Brass Contributor)
Solution
Sure, should be doable via PowerShell, try something like this:

foreach ($user in $users) {
Add-DistributionGroupMember ($user.department + $user.office) -Member $user.UserPrincipalName
}

where I'm assuming you're passing a user object (Get-User), so the aforementioned properties are readily available.

View solution in original post