0

I have the script below to create a SharePoint Group in a SharePoint Online site using CSOM.

Is there a way to change it in order to create various groups instead of only one and to specify the group owner for each group?

$host.Runspace.ThreadOptions = "ReuseThread" #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site function 

Create-SPOGroup { 
param($sSiteColUrl,$sUsername,$sPassword,$sGroupToCreate,$sGroupToCreateDescription) 
try { 
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 
Write-Host "Creating SharePoint Group $sGroupToCreate in $sSiteColUrl" -foregroundcolor Green 
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 

#Adding the Client OM Assemblies 
Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.Runtime.dll" 

#SPO Client Object Model Context 
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword) 
$spoCtx.Credentials = $spoCredentials 

#Root Web Site 
$spoRootWebSite = $spoCtx.Web 

#Object for creating a new SPO Group 
$spoGroupCreationInfo= New-Object Microsoft.SharePoint.Client.GroupCreationInformation 
$spoGroupCreationInfo.Title=$sGroupToCreate 
$spoGroup=$spoRootWebSite.SiteGroups.Add($spoGroupCreationInfo) 
$spoCtx.ExecuteQuery() 

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 
Write-Host "SharePoint Group $sGroupToCreate in $sSiteColUrl created succesfully!!" -foregroundcolor Green 
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 
$spoCtx.Dispose() 
} 
catch [System.Exception] { 
   write-host -f red $_.Exception.ToString() 
} 
} 
#Required Parameters 
$sSiteColUrl = "test" 
$sUsername = "testuser" 
#$sPassword = Read-Host -Prompt "password" -AsSecureString 
$sPassword=convertto-securestring "password" -asplaintext -force 
$sGroupToCreate="CustomSPOGroup" 
Create-SPOGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroupToCreate $sGroupToCreate
2

I am a big fan of using the PnP-PowerShell cmdlets for jobs like this: https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/readme.md

You can use New-PnPGroup to create a new group, assigning all the same values you get in the GUI. You can then pipe this to Set-PnPGroupPermissions to assign rights to a particular site.

Connect-PnPOnline -Url $siteurl -Credentials $creds
$newGroup = New-PnPGroup -Title $GroupTitle -Owner $OwnerEmail -Description $GroupDescription
Set-PnPGroupPermissions -Identity $newGroup -AddRole $AddRole
Disconnect-PnpOnline

You could then feed it a file with group names, permissions and sites to apply the group to, and use foreach to cycle through them.

EDIT with full script (untested)

Install instructions are here: https://github.com/SharePoint/PnP-PowerShell/blob/master/README.md

Install the cmdlets, then create a .csv file with the information you want, like this.

siteurl,grouptitle,owneremail,groupdescription,addrole
https://tenancy.sharepoint.com/sites/site1,testgroup1,owner@tenancy.com,"This is a group description",Contribute
https://tenancy.sharepoint.com/sites/site2,testgroup2,owner2@tenancy.com,"This is another group",Read

Then run the following

$groups = import-csv "c:\pathtocsv\csvname.csv"
$creds = Get-Credential
foreach ($group in $groups){
    # Set up variables
    $siteurl = $group.siteurl
    $GroupTitle = $group.grouptitle
    $OwnerEmail = $group.owneremail
    $GroupDescription = $group.groupdescription
    $addrole = $group.addrole

    # Connect to site
    Connect-PnPOnline -Url $siteurl -Credentials $creds
    #Create Group
    $newGroup = New-PnPGroup -Title $GroupTitle -Owner $OwnerEmail -Description $GroupDescription
    # Set Permissions on group
    Set-PnPGroupPermissions -Identity $newGroup -AddRole $AddRole
    # Disconnect from site
    Disconnect-PnpOnline
}
  • I cannot make it work... is there any other way to do this? – Peter Smp May 31 '17 at 9:40
  • What issues are you having? Are you getting any error messages? – Gizmo3k May 31 '17 at 12:03
  • The thing is that I don't know how to do it.. never worked with pnp before – Peter Smp May 31 '17 at 12:05
  • Could you please help? – Peter Smp May 31 '17 at 12:17
  • @PeterSmp answer updated, hope this helps – Gizmo3k Jun 1 '17 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.