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:

I got to create a bunch of 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 -UseWebLogin
    $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
}

Here is my CSV File format:

powershell to create multiple folders in sharepoint online document library from CSV file

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:

PowerShell to Create folders sub-folders from CSV file in SharePoint Online
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFilePath = "C:\Temp\Folders.csv"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin

    #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
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

10 thoughts on “SharePoint Online: PowerShell to Bulk Create Folders in a Document Library from a CSV File

  • March 19, 2021 at 12:50 PM

    Resolve-PnPFolder : Access denied. You do not have permission to perform this action or access this resource

    Reply
  • March 18, 2021 at 6:12 PM

    This is great, just what I was looking for. I am looking to setfolder level permissions from csv file too.

    Reply
  • March 9, 2021 at 4:05 PM

    Same here.. Access denied

    Reply
  • March 2, 2021 at 5:04 PM

    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.

    Reply
  • February 25, 2021 at 9:37 PM

    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:

    Reply
  • February 20, 2021 at 10:15 PM

    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.

    Reply
  • December 18, 2020 at 2:23 PM

    Error: Cannot bind argument to parameter ‘SiteRelativePath’ because it is null.

    Why is that happening?

    #Config Variables
    $SiteURL = “https://crescent.sharepoint.com/sites/marketing”
    $CSVFilePath = “C:TempFolders.csv”

    Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin

    #Get the CSV file
    $CSVFile = Import-Csv $CSVFilePath

    #Read CSV file and create document document library
    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
    }

    Reply
  • August 14, 2020 at 11:21 AM

    is there any Powershell script to create folders and subfolders from CSV in a document library

    Reply

Leave a Reply