SharePoint Online: PowerShell to Bulk Create Folders in a Document Library from a CSV File
Requirement: Create Multiple Folders in a Document Library from a CSV File in SharePoint Online.
PowerShell to Bulk Create Folders from a CSV File in SharePoint Online:
Are you looking to create multiple folders in SharePoint Online, but don’t want to do it one at a time? Maybe you want to create a folder structure for a new project or sort your documents into different categories. This article will show you how to use PowerShell to create multiple folders quickly.
I got to create several folders in a SharePoint Online document library! This PowerShell script takes folder names from a CSV file and creates folders in the specified document library.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Ops"
$CSVFilePath = "C:\Temp\Folders.csv"
$LibraryName = "Documents"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Web = Get-PnPWeb
#Get the Document Library and its site relative URL
$Library = Get-PnPList -Identity $LibraryName -Includes RootFolder
If($Web.ServerRelativeUrl -eq "/")
{
$LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl
}
else
{
$LibrarySiteRelativeURL = $Library.RootFolder.ServerRelativeUrl.Replace($Web.ServerRelativeUrl,'')
}
#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath
#Read CSV file and create folders
ForEach($Row in $CSVFile)
{
#Replace Invalid Characters from Folder Name, If any
$FolderName = $Row.FolderName
$FolderName = [RegEx]::Replace($FolderName, "[{0}]" -f ([RegEx]::Escape([String]'\"*:<>?/\|')), '_')
#Frame the Folder Name
$FolderURL = $LibrarySiteRelativeURL+"/"+$FolderName
#Create Folder if it doesn't exist
Resolve-PnPFolder -SiteRelativePath $FolderURL | Out-Null
Write-host "Ensured Folder:"$FolderName -f Green
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This will save you a lot of time and ensure a more consistent folder structure across your site. Here is my CSV File format:
You can download the CSV file here:
PowerShell to Create folders and Sub-folders from CSV file in SharePoint Online
How about creating folders and sub-folders in multiple document libraries? Here is my CSV format:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFilePath = "C:\Temp\Folders.csv"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath
#Read CSV file and create folders
ForEach($Row in $CSVFile)
{
#Create Folder if it doesn't exist
Resolve-PnPFolder -SiteRelativePath $Row.FolderSiteRelativeURL | Out-Null
Write-host "Ensured Folder:"$Row.FolderSiteRelativeURL -f Green
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
If you want to clone a local folder structure to your SharePoint Online site, use: Upload Folder Structure to SharePoint Online using PowerShell
Is there anyway to create a folders in multiple sites at once using script ?
Hi!
I’m trying to run the script but I had to change the connexion string to “Connect-SPOService -URL” and I got the error message “Error: No valid OAuth 2.0 authentication session exists” I’ve tried everything and still no luck. Any idea? We are using MFA
Hi
I’m trying to create a folder across multiple (51) document libraries. I would like to use a CSV file to feed in the document library and folder location. Would this be possible with the above script/procedure? If so, what alterations should I make?
Hope you can help.
Sure! You can use the last script in this post.
Anyone have any way to add folders, but at the same time ‘remove inherited permissions’ so each created folder does not use parent folder permissions?
Sure, Here is your reference: PowerShell to Break Folder Permission Inheritance in SharePoint Online
I got the following error:
Write-host “Ensured Folder:”$Row.FolderSiteRelativeURL -f Green
| ~~~~~~~
| Unexpected token ‘Ensured’ in expression or statement.
Resolve-PnPFolder : Access denied. You do not have permission to perform this action or access this resource
Try elevating the user permissions to Site Owner.
This is great, just what I was looking for. I am looking to setfolder level permissions from csv file too.
Looking for help, want to setfolder level permissions from a csv file once the above is complete, has anyone achieved or seen articles on this.
Thanks for sharing your knowledge, the script was executed but no folder was created and I received warning message “Consider using -Interactive instead, which provides better functionality. See the documentation at https://pnp.github.io/powershell/cmdlets/connect-pnponline.html#interactive-login-for-multi-
factor-authentication
Ensured Folder:
Ensured Folder:
Ensured Folder:
I was getting the same result until I added FolderSiteRelativeURL as the first row of my CSV file.
Now I am getting the following if anyone can help with that.
Access denied. You do not have permission to perform this action or access this resource.
Error: Cannot bind argument to parameter ‘SiteRelativePath’ because it is null.
Why is that happening?
I had this error, i did not have the correct name in the CSV file ‘FolderSiteRelativeURL’ Also the script did not like friendly folder names in the CSV file.
is there any Powershell script to create folders and subfolders from CSV in a document library
Sure! Article has been updated.
Nice, thanks!