question

frob avatar image
0 Votes"
frob asked JoyZ commented

Is there any PowerShell that can read from a CSV file and create SharePoint groups and add members like this?

Hi there

Is there any PowerShell that can read from a CSV file and create SharePoint groups and add members? CSV file looks like this:

Users, SharePointGroupName
John@contoso.com, Group 1
John@contoso.com, Group 2
John@contoso.com, Group 3
Mary@contoso.com, Group 1
Mary@contoso.com, Group 4
Peter@contoso.com, Group 3
Peter@contoso.com, Group 4
Peter@contoso.com, Group 5
and so on...

Thank you.




office-sharepoint-onlinewindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JoyZ avatar image
0 Votes"
JoyZ answered JoyZ commented

@frob,

We can create SharePoint groups from a csv file first, and then use another csv file to add users.

1.Open Notepad, and paste the following text block into it(where tenant equals your tenant name):

 Site,Group,PermissionLevels
 https://tenant.sharepoint.com/sites/Team1,Group1,Full Control
 https://tenant.sharepoint.com/sites/Team1,Group2,View Only
 https://tenant.sharepoint.com/sites/Team1,Group3,Design
 https://tenant.sharepoint.com/sites/Team1,Group4,Full Control
 https://tenant.sharepoint.com/sites/Team1,Group5,Edit

2.Save the file to your desktop as GroupsAndPermissions.csv

3.Open a new instance of Notepad, and paste the following text block into it:

 Group,LoginName,Site
 Group1,user1@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group2,user2@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group3,user3@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group4,user4@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group5,user5@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group1,user5@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1
 Group1,user3@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Team1

Where tenant equals your tenant name, and username equals the user name of an existing user.

4.Save the file to your desktop as Users.csv
5.Then run the following powershell to create groups and add members from CSV file:

 #Variables for processing
 $AdminURL = "https://tenant-admin.sharepoint.com/"
 $AdminName = "julie@tenant.onmicrosoft.com"
      
 #User Names Password to connect
 $Password = Read-host -assecurestring "Enter Password for $AdminName"
 $Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password
    
 #Connect to SharePoint Online
 Connect-SPOService -url $AdminURL -credential $Credential
    
 Import-Csv C:\users\MyAlias\desktop\GroupsAndPermissions.csv | ForEach-Object {New-SPOSiteGroup -Group $_.Group -PermissionLevels $_.PermissionLevels -Site $_.Site}
 Import-Csv C:\users\MyAlias\desktop\Users.csv | where {Add-SPOUser -Group $_.Group –LoginName $_.LoginName -Site $_.Site}

Simple resullt for your reference:

121852-image.png

121765-image.png

More information for your reference:

https://docs.microsoft.com/en-us/microsoft-365/enterprise/create-sharepoint-sites-and-add-users-with-powershell?view=o365-worldwide


If an Answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (11.1 KiB)
image.png (52.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@frob,

I’m checking how the things are going on about this issue.

Please feel free to reply if there is any update.

0 Votes 0 ·