Forum Discussion
Vincent ORTOLLAND
Oct 26, 2018Copper Contributor
How to create O365 Groups in Powershell from a csv file
Hello,
First I want to say that it's my irst post here, so I didn't know if I should post here or in the Powershell conversations.
I'm struggling at the moment because I'm trying to massively create a few hundreds Office 365 groups for my organization, each with (possibly) multiple owners and members.
I've got a list of the groups to create in a csv file, which looks like that:
Name;Email;Members;Owners
O365 Test Group Vincent;O365_TestGroup_vincent@xxx.fr;e-mail_of_member1, e-mail_of_member2;e-mail_of_owner1, e-mail_of_owner2
(of course in my actual script I have the actual e-mail addresses)
My script looks like that:
$Groups=Import-Csv "path to my csv file" -Header Name, Email, Members, Owners -Delimiter ";" | select -skip 1
foreach ($Group in $Groups){
$Name=$Group.Name
$Mail=$Group.Email
$Members=$Group.Members
$Owner=$Group.Owners
New-UnifiedGroup -DisplayName $Name -EmailAddresses $Mail -ManagedBy $Owner -Members $Members -AccessType Private -Language fr-FR -RequireSenderAuthenticationEnabled $false -SubscriptionEnabled -AutoSubscribeNewMembers
}
It seems that adding multiple members or owners in a variable from a text file does not work like that.
It returns me this error (in French, but it says that "user1, user2" cannot be found)
Désolé... Nous n’avons pas pu trouver le propriétaire «e-mail_of_owner1, e-mail_of_owner2», car nous avons rencontré l’erreur «L'objet
"user1, user2" est introuvable. Assurez-vous que vous l'avez correctement tapé ou spécifiez un autre objet.».
+ CategoryInfo : NotSpecified: (:) [New-UnifiedGroup], GroupMailboxFai...eOwnerException
+ FullyQualifiedErrorId : [Server=MR2P264MB0033,RequestId=036653cc-558d-407c-9e71-84ce2c437b53,TimeStamp=26/10/2018 12:12:42] [FailureCategory=Cmdlet-GroupMailboxFailedToResolveOwnerException] 4C7DBAFB,Microsoft.Exchang
e.Management.RecipientTasks.NewUnifiedGroup
+ PSComputerName : ps.outlook.com
If I try without the import-csv part and I put multiple owners separated by a comma in my variable, it works, I can create one group like that (but that means 1 script for 1 group)
So I don't really know how to do it with my list of groups in a text file.
Can you help me with that please?
Thanks in advance.
Best regards,
Vincent
The reason for the failure is PowerShell treating the value in that column as string, and not a list. You should be able to do it like this:
Add-UnifiedGroupLinks groupname -LinkType Member -Links $_.Members.Split(",")
You can adapt the example for the New-UnifiedGroup cmdlets as well, it's simply easier to test with the Add-UnifiedGroupLinks one.
The reason for the failure is PowerShell treating the value in that column as string, and not a list. You should be able to do it like this:
Add-UnifiedGroupLinks groupname -LinkType Member -Links $_.Members.Split(",")
You can adapt the example for the New-UnifiedGroup cmdlets as well, it's simply easier to test with the Add-UnifiedGroupLinks one.
- Vincent ORTOLLANDCopper Contributor
Hello,
It works perfectly.
I tried to adapt my own script and with just a -split when declaring my variables, it works as well.
Thank you very much, you helped me greatly.
Vincent
- BeuferdCopper Contributor
Vincent ORTOLLAND
Can you show the finished script?
I don't know how to make the adjustment to your original script.