question

frob avatar image
0 Votes"
frob asked EchoDu-MSFT commented

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

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.

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

EchoDu-MSFT avatar image
0 Votes"
EchoDu-MSFT answered EchoDu-MSFT commented

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.



splib.png (21.3 KiB)
powershell.png (206.6 KiB)
csv.png (45.3 KiB)
powershell.png (43.8 KiB)
csv.png (34.5 KiB)
· 4
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.

Hi EchoDu,

Sorry, I could not understand this. Not sure why so much information is needed. All I need is to create several document libraries taking their names from a CSV file.

Please let me know if I misunderstood something.

Thank you so much, your time is appreciated.

0 Votes 0 ·

Hi @frob ,

Thanks for your reply.

I have updated answer under initial My Answer. Hope this can help you to solve issue.

Have a nice day!

Thanks,
Echo Du

0 Votes 0 ·

Hi EchoDu,

Your time is so much appreciated. It seems there is a communication gap here. I do not need any files to be created at all. I need empty document libraries. The Title of the libraries needs to be imported from a CSV file.

DocumentLibrary1
DocumentLibrary2
DocumentLibrary3
and so on...

The script should be simple like:

-//Load the CSV file
//Foreach line
//Create an empty Document LIbrary with a command like New-PnPList -Title $documentLibrarNameToCreateUsingPnP -Template DocumentLibrary -OnQuickLaunch

Thank you.

0 Votes 0 ·
Show more comments