Avatar of Stephen Frendl
Stephen Frendl
Flag for United States of America asked on

How can I create new sub-folders in-bulk to all of my SharePoint folders all at once? (not one at a time)

I am working with a SharePoint site, there are about 2500 folders in question, and I need to add a new sub-folder within each of them with the same title. Other than creating this new sub-folder in each of these folders one at a time, what are my options to automate/add this folder in bulk?

Microsoft SharePoint

Avatar of undefined
Last Comment
Stephen Frendl

8/22/2022 - Mon
Rainer Jeschor

Hi Stephen,
first question: SharePoint On-Premises (if yes, which version) OR SharePoint Online?
second question: are all of these folders in one site / one document library?

Probably the best way would be using a PowerShell script - but depending on the SharePoint version either PnP-Powershell module (SharePoint Online) or some kind of SharePoint Client Object model in PowerShell.

E.g. for SharePoint Online:
Add-PnPFolder -Name NewFolder -Folder "Shared Documents/Folder"

Open in new window


HTH and KR
Stephen Frendl

Hi Rainer,
Thanks for your response.

I am using SharePoint Online. And yes, they are all under one of the pages on the Sharepoint site. On the left side, it goes:

Home, Documents, Relationships, Shared with Us, Recycle Bin.

"Relationships" is the main folder that contains all the sub-folders in which I want to add a single new folder to each.
Rainer Jeschor

Hi Stephen,

did a quick demo script:
Get-PnPFolder -List "Relationships" | foreach-object { $newFolderPath = $_.ServerRelativeUrl.Replace("/sites/deploytarget/","") + "/YYY"; Resolve-PnPFolder -SiteRelativePath $newFolderPath}

Open in new window

This is a one liner for immediate execution in Powershell terminal.
It gets all folders in the list "Relationships" and then for each folder it calls the Resolve-PnPFolder which creates the folder if it does not yet exist. The "issue" is that the function needs the site relative path, therefore I replaced the "/site/sitename/" with "" and added the subfoldername "/YYY"

How did I do that?
I used Windows Terminal with Powershell 7.
I installed the Powershell module "PnP.Powershell"
Install-Module -Name PnP.PowerShell

Open in new window

Then I connect to the target SharePoint Site Collection (which is not necessarily the Root site collection):
Connect-PnPOnline -Url https://tenant.sharepoint.com/sites/deploytarget

Open in new window

In the site collection deploytarget I have a newly created document library named "Relationships". In there I created manually three folders.

Pre-Script execution:


Some warnings:
The Get-PnPFolder with the list parameter will return ALL FOLDERS of the library - so if you already have subfolders in there, then the script needs to be adjusted to verify that the folder is in the root of the library.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Stephen Frendl

This sounds really promising! Thanks very much for doing all of this.

I have followed your steps, however I get this when I try to login with the admin credentials:

Connect-PnPOnline: AADSTS65001: The user or administrator has not consented to use the application with ID '31359c7f-bd7e-475c-86db-fdb8c937548e' named 'PnP Management Shell'. Send an interactive authorization request for this user and resource.

Thoughts on my nexts steps?

As per your warning: Within "Relationships", there are those 2,505 folders, and in each of those there are many other subfolders. Is that what you mean by "if you already have subfolders in there"?

Thank you!
Rainer Jeschor

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Stephen Frendl

Sir, you are brilliant. This ultimately worked!

Thank you so much, Rainer!