Link to home
Create AccountLog in
Avatar of Stephen Frendl
Stephen FrendlFlag 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?

Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

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
Avatar of 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.
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

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:
User generated image
User generated image
User generated imageSome 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.

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!
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sir, you are brilliant. This ultimately worked!

Thank you so much, Rainer!