Azure AD & PowerShell How To: Add multiple users or a group into multiple groups?

%3CLINGO-SUB%20id%3D%22lingo-sub-150304%22%20slang%3D%22en-US%22%3EAzure%20AD%20%26amp%3B%20PowerShell%20How%20To%3A%20Add%20multiple%20users%20or%20a%20group%20into%20multiple%20groups%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-150304%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Forum%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20have%20a%20bunch%20of%20users%20(Many%20users)%2C%20and%20I%20want%20to%20add%20all%20them%20into%20many%20multiple%20Azure%20AD%20security%20groups%20(Nothing%20On-Prem)%2C%20Something%20like%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EUsers%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EUser1%2CUser2%2CUser3...etc.%20%3D%26gt%3B%20Of%20course%2C%20I%20can%20add%20all%20these%20users%20into%20one%20security%20group%20e.g.%20called%20%22%3CSPAN%3E%3CSTRONG%3EAll%3C%2FSTRONG%3E%22%20group.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGroups%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EAll%20users%20%3CSPAN%3E(Or%2C%20%3CSTRONG%3EAll%3C%2FSTRONG%3E%20Group)%26nbsp%3B%3C%2FSPAN%3Eare%20added%20into%3A%20Group1%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAll%20users%20(Or%2C%20%3CSTRONG%3EAll%3C%2FSTRONG%3E%20Group)%20are%20added%20into%3A%20Group2%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAll%20users%20(Or%2C%20%3CSTRONG%3EAll%3C%2FSTRONG%3E%20Group)%20are%20added%20into%3A%20Group3%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E...etc.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt%20is%20so%20hard%20to%20perform%20this%20operation%20manually%2C%20and%20I%20tried%20with%20PowerShell%20below%20but%20it%20keeps%20failing.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24Allusers%20%3D%20Import-Csv%20%22C%3A%5Ctest%5Cusers.csv%22%3CBR%20%2F%3E%24secgrp%20%3D%20Import-Csv%20%22C%3A%5Ctest%5Cgroups.csv%22%3CBR%20%2F%3E%24Sgroup%20%3D%20%40()%3CBR%20%2F%3E%24secgrp%20%7C%20ForEach-Object%20%7B%20%24Sgroup%20%2B%3D%20%24_.group%20%7D%3CBR%20%2F%3Eforeach%20(%24grp%20in%20%24sgroup)%20%7B%3CBR%20%2F%3E%24GP%20%3D%20Get-ADGroup%20%24grp%3CBR%20%2F%3Eforeach%20(%24user%20in%20%24Allusers)%20%7B%20Try%3CBR%20%2F%3E%7B%20%24Aduser%20%3D%20Get-ADUser%20-Identity%20%24user.Accounts%20-ErrorAction%20SilentlyContinue%3CBR%20%2F%3Eif%20(%24Aduser%20-ne%20%24null)%20%7B%3CBR%20%2F%3EAdd-ADGroupMember%20%24GP%20-Members%20%24Aduser.SamAccountName%20-Confirm%3A%24false%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3Ecatch%20%7B%20%24Error%5B0%5D.ToString()%20%7C%20Out-File%20%22C%3A%5Ctest%5Cuserlog.txt%22%20-Append%20-Force%20%7D%3C%2FP%3E%0A%3CP%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECould%20you%20please%20help%20me%20out%20adding%20all%20these%20users%20or%20the%20group%20that%20contains%20them%20all%20into%20all%20these%20Azure%20AD%20security%20groups%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-150304%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20AD%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Contributor

Hello Forum

 

I have a bunch of users (Many users), and I want to add all them into many multiple Azure AD security groups (Nothing On-Prem), Something like:

 

Users:

User1,User2,User3...etc. => Of course, I can add all these users into one security group e.g. called "All" group.

 

Groups:

All users (Or, All Group) are added into: Group1

All users (Or, All Group) are added into: Group2

All users (Or, All Group) are added into: Group3

...etc.

 

It is so hard to perform this operation manually, and I tried with PowerShell below but it keeps failing.

 

$Allusers = Import-Csv "C:\test\users.csv"
$secgrp = Import-Csv "C:\test\groups.csv"
$Sgroup = @()
$secgrp | ForEach-Object { $Sgroup += $_.group }
foreach ($grp in $sgroup) {
$GP = Get-ADGroup $grp
foreach ($user in $Allusers) { Try
{ $Aduser = Get-ADUser -Identity $user.Accounts -ErrorAction SilentlyContinue
if ($Aduser -ne $null) {
Add-ADGroupMember $GP -Members $Aduser.SamAccountName -Confirm:$false
}
}
catch { $Error[0].ToString() | Out-File "C:\test\userlog.txt" -Append -Force }

}
}

 

Could you please help me out adding all these users or the group that contains them all into all these Azure AD security groups?

 

0 Replies