Home > Enterprise >  How to create M365 dynamic groups from CSV file using powershell
How to create M365 dynamic groups from CSV file using powershell

Time:06-01

I've been tasked with creating 1600 O365 dynamic groups for our Azure AD deployment. We currently have all the syntax/group rules created for these groups in a CSV file. My problem is: i'm using the following script to try to create these groups

Connect-AzureAD
$Groups = Import-Csv -Path C:\Temp\AzureAD_Groups.csv
$dynamicGroupTypeString = "DynamicMembership"

foreach($Group in $Groups)
{
New-AzureADGroup -DisplayName $Group.DisplayName -Description $Group.Description -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -membershipRule $Group.MembershipRule -GroupTypes $dynamicGroupTypeString
} 

After running this script i get the error:

New-AzureADGroup : A parameter cannot be found that matches parameter name 'membershipRule'.
At line:8 char:147
  ...  -MailNickName "group" -SecurityEnabled $True -membershipRule $Group. ...

I looked into this error and i see that this flag/parameter doesnt exist for New-AzureADGroup and i also saw that it works for Set-AzureADGroup. My question is: instead of creating then setting the group rules after, can i perform this task all in one script so it creates the groups then uses the pre-made syntax in my CSV?

My CSV file is setup as such: enter image description here

Any help is much appreciated, thanks.

CodePudding user response:

I tried to reproduce the same in my environment and got the same error like below:

enter image description here

My Imported CSV file looks like below:

enter image description here

Please note that, you have to use New-AzureADMSGroup command to create M365 Dynamic group. Make sure to include -MembershipRuleProcessingState "On" while using that command.

I'm able to create dynamic groups successfully after changing the script like below:

Connect-AzureAD
$Groups = Import-Csv -Path "C:\test\AzureAD_groups.csv"
$dynamicGroupTypeString = "DynamicMembership"

foreach($Group in $Groups)
{
New-AzureADMSGroup -DisplayName $Group.DisplayName -Description $Group.Description -MailEnabled $False -MailNickName "group" -SecurityEnabled $True -membershipRule $Group.MembershipRule -GroupTypes $dynamicGroupTypeString -MembershipRuleProcessingState "On"
} 

enter image description here

M365 dynamic groups created successfully like below:

enter image description here

Members are dynamically allotted to groups based on membership rules like below:

enter image description here

enter image description here

  • Related