I've found a script to create document libraries and a script to assign permissions to a list. I've combined these with one script and added a CSV containing names and Office 365 Groups that should be added to the list.

The purpose of the script is to execute the following tasks:

  1. Read the CSV file line by line (Foreach)
  2. Set DocLib Name and Off365Group
  3. Create library with DocLib as name and description
  4. Add Domain admins (Office 365 Group) as owner to the list
  5. Add CustomerLocation-DocLib (Office 365 Group) as contributor to the list

Part 1 - 3 are working As for part 4/5, I think that the script is currently polling the SharePoint Groups of the site, not Office Groups.

My question is, how can I extend the script to use Office Groups. The user management will be done through the Admin Panel of Office 365 by the customer.


PS: I take no credit for the script because it is created with samples found trough Google :-)

// Script

$siteURL = "https://contoso.sharepoint.com/sites/test"
$userName = "[email protected]"
$Password = "SuperSecretPassword"

# ----------------------------------------------------------------------------

$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition

Add-Type -Path "C:\Git\office-365\Losse Apps\365App\lib\SharePointOnline\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Git\office-365\Losse Apps\365App\lib\SharePointOnline\Microsoft.SharePoint.Client.Runtime.dll"

function Add-SPPermissionToListGroup
    param (

    #$web = Get-SPOWeb -Identity $Url

    $list = $Context.web.Lists.GetByTitle($ListName)

    if ($list -ne $null)
        # Ensure that the permissions are not being inherited.
        if ($list.HasUniqueRoleAssignments -eq $False)

        # Modify the permissions.
        if ($Context.web.SiteGroups[$GroupName] -ne $null)
            $group = $Context.web.SiteGroups[$GroupName]
            $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            $roleDefinition = $Context.web.RoleDefinitions[$PermissionLevel];
            Write-Host "Successfully added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
            Write-Host "Group $GroupName does not exist." -foregroundcolor Red


Function CreateDocumentLibrary {

    Param (

    $listTitle = $Name
    $listDescription = $Name
    $listTemplate = 101

    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCred

    #create list using ListCreationInformation object (lci)
    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.title = $listTitle
    $lci.description = $listDescription
    $lci.TemplateType = $listTemplate
    $list = $context.web.lists.add($lci)

    #send the request containing all operations to the server
    try {
        write-host "info: Created $($listTitle)" -foregroundcolor green

    catch {
        write-host "info: $($_.Exception.Message)" -foregroundcolor red

$SPOPass = ConvertTo-SecureString $Password -AsPlainText -Force
$SPOCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $SPOPass)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$Context.credentials = $SPOCred

Import-CSV $ScriptPath\DocumentLibraries.csv | ForEach-Object {
    $DocLib = $_.DocumentLibrary
    $Group = $_.Group

    #CreateDocumentLibrary -Name $DocLib
    Add-SPPermissionToListGroup -Url $siteURL -ListName $DocLib -GroupName 'Domain Admins' -PermissionLevel 'Full Control'
    Add-SPPermissionToListGroup -Url $siteURL -ListName $DocLib -GroupName $Group -PermissionLevel Contribute



// CVS

Test1, CustomerLocation-Doc-Test1
Test2, CustomerLocation-Doc-Test2
Test3, CustomerLocation-Doc-Test3
Test4, CustomerLocation-Doc-Test4
Test5, CustomerLocation-Doc-Test5
  • What do you mean by Office 365 Groups? Security Groups or Unified Groups? If Unified Groups then unfortunately no - it is not possible.
    – grisha
    Commented Sep 21, 2016 at 11:10
  • No I meant security groups
    – Paul_dH
    Commented Oct 23, 2016 at 15:21

1 Answer 1


Check out the Office Dev PNP for PowerShell. https://github.com/SharePoint/PnP-PowerShell. You can get this done in a few lines vs all that custom CSOM.

Connect-PnpOnline https://tenant.sharepoint.com
New-PnPList -Title NameMe -Template Library
New-PnPGroup -Title GroupName
Set-PnPGroupPermissions -Identity GroupName -List NameMe -AddRole Contribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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