About SharPoint

ネパリ サンデャ 520 Reputation points
2023-06-13T02:17:10.8233333+00:00

Is there any way to create Bulk creation of document library through powershell

as well as give access right to the user through Powershell? I have search and found one document with the script but the script was hard to understand for me

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

Answer accepted by question author
  1. Yanli Jiang - MSFT 31,641 Reputation points Microsoft External Staff
    2023-06-13T08:45:53.8966667+00:00

    Hi @ネパリ サンデャ ,

    You can use the follow script to bulk create document library:

     
          
    #Function to Create a SharePoint Online document library
    Function CreateSPO-DocumentLibrary()
    {
     
     [cmdletbinding()]
     
        param
        (
            [Parameter(Mandatory=$True,ValueFromPipeline)] [String[]] $LibraryNames,
            [Parameter(Mandatory=$False)] [String] $Description
        )   
        Try {
            #Create Each Document Library
            ForEach($LibraryName in $LibraryNames)
            {
                Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
              
                #Get All Existing Lists from the web
                $Lists = $Web.Lists
                $Ctx.Load($Lists)
                $Ctx.ExecuteQuery()
      
                #Check if Library name doesn't exist already
                If(!($Lists.Title -contains $LibraryName))
                {
                    #Create a new Document Library
                    $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                    $ListInfo.Title = $LibraryName
                    $ListInfo.Description =  $Description
                    $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary
                    $Web.Lists.Add($ListInfo) | Out-Null
                    $Ctx.ExecuteQuery()
       
                    write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
                }
                Else
                {
                    Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
                }
            }
        }
        Catch {
            write-host -f Red "`tError:" $_.Exception.Message
        }
    }
      
    #Set Parameters
    $SiteURL= "https://domain.sharepoint.com/sites/sitename/"
      
    #Get Credentials to connect
    $Cred = Get-Credential
      
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        
    #Get the web from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()
      
    #Call the function to create document libraries
    CreateSPO-DocumentLibrary -LibraryNames @("Document Library 1", "Document Library 2", "Document Library 3", "Document Library 4")   
    

    Please fill in $SiteURL and LibraryNames according to your actual situation.

    You can use the follow script to grant access to a SharePoint Online site level:

    
    
        
    #Variables for Processing
    $SiteURL = "https://domain.sharepoint.com/Sites/sitename/"
    $UserAccount = "******@domain.com"
    $PermissionLevel = "Contribute"
     
    #Setup Credentials to connect
    $Cred = Get-Credential
     
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
           
        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()
     
        #Ensure the user
        $User=$web.EnsureUser($UserAccount)
        $Ctx.Load($User)
        $Ctx.ExecuteQuery()
     
        #Get the Permission Level 
        $RoleDefinition = $web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleAssignment = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleAssignment.Add($RoleDefinition) 
         
        #Assign Role Assignment to User
        $Permissions = $Web.RoleAssignments.Add($User,$RoleAssignment)
        $Web.Update()
        $Ctx.ExecuteQuery()
       
        Write-host  -f Green "User '$UserAccount' has been Granted with Access '$PermissionLevel'!"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.