Is there any PowerShell to create several Document libraries getting their names from a CSV file? (SharePoint Online)

frob 4,261 Reputation points
2021-08-10T18:51:02.193+00:00

Hi there

For SharePoint Online site:
Is there any PowerShell to create several Document libraries getting their names from a CSV file?
CSV file looks like this:

DocLib1
DocLib2
DocLib3
DocLib4
.. and so on...

Thank you.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Echo Du_MSFT 17,316 Reputation points
    2021-08-11T02:56:15.077+00:00

    Hi @frob ,

    Please run the below PnP powershell script as an admin:

    #Parameters  
    $SiteURL = "https://crescent.SharePoint.com/sites/sitename"  
    $ListName= "libraryname"  
    $ReportOutput = "C:\Temp\mex-DocInventory.csv"  
    $Pagesize = 500  
         
    #Connect to SharePoint Online site  
    Connect-PnPOnline $SiteURL -Credentials (Get-Credential)  
       
    #Delete the Output report file if exists  
    If (Test-Path $ReportOutput) { Remove-Item $ReportOutput}  
       
    #Array to store results  
    $Results = @()  
         
    #Get all Documents from the document library  
    $List  = Get-PnPList -Identity $ListName  
    $global:counter = 0;  
    $ListItems = Get-PnPListItem -List $ListName -PageSize $Pagesize -Fields Author, Editor, Created, File_x0020_Type -ScriptBlock `  
            { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `  
                 "Getting Documents from Library '$($List.Title)'" -Status "Getting Documents data $global:Counter of $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}  
        
    $ItemCounter = 0  
    #Iterate through each item  
    Foreach ($Item in $ListItems)  
    {  
            $Results += New-Object PSObject -Property ([ordered]@{  
                Name              = $Item["FileLeafRef"]  
                Type              = $Item.FileSystemObjectType  
                FileType          = $Item["File_x0020_Type"]  
                RelativeURL       = $Item["FileRef"]  
                CreatedByEmail    = $Item["Author"].Email  
                CreatedOn         = $Item["Created"]  
                Modified         = $Item["Modified"]  
                ModifiedByEmail    = $Item["Editor"].Email  
            })  
        $ItemCounter++  
        Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Exporting data from Documents $ItemCounter of $($List.ItemCount)" -Status "Exporting Data from Document '$($Item['FileLeafRef'])"          
    }  
        
    #Export the results to CSV  
    $Results | Export-Csv -Path $ReportOutput -NoTypeInformation  
         
    Write-host "Document Library Inventory Exported to CSV Successfully!"  
    

    122181-splib.png

    122028-powershell.png

    122088-csv.png

    Thanks,
    Echo Du

    ======================
    Updated Answer =====================

    Hi @frob ,

    Please run the below PnP powershell script as an admin:

    #Set Variables  
    $SiteURL = "https://crescent.sharepoint.com/sites/sitename"  
    $ReportOutput = "C:\Temp\DocumentsLibraryReport.csv"  
      
    #Connect to PNP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
      
    #Get all document libraries - Exclude Hidden Libraries  
    $DocumentLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false} #Or $_.BaseType -eq "DocumentLibrary"  
       
    #Get Document Libraries Name, Default URL and Number of Items  
    $DocumentLibraries | Select Title, DefaultViewURL, ItemCount | Export-CSV $ReportOutput  
    

    122580-powershell.png

    122672-csv.png

    Thanks,
    Echo Du

    ========================

    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.


0 additional answers

Sort by: Most helpful

Your answer

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