Recently, I was working in a SharePoint portal to organize documents in document libraries. For this, I used PowerShell to create some folders and subfolders. In this tutorial, I will explain how to create folders and subfolders in a SharePoint Online document library using PnP PowerShell.
Here, we will use the PnP PowerShell to create folders or subfolders inside a document library in SharePoint.
Create a Single Folder in a Document Library using PnP PowerShell
In the first example, I will explain how to create a single folder in a SharePoint document library using PnP PowerShell.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Trainings" -ClientId "a03427ad-e13c-dsfhhjjj-4rfgh-ab35ed43e421" -Interactive
# Variables
$libraryPath = "/sites/Trainings/Shared Documents"
$newFolderName = "ProjectA"
# Create folder
Add-PnPFolder -Name $newFolderName -Folder $libraryPath
Now, when you open the SharePoint document library, you can see the folder created successfully.

Check out Get SharePoint Online Site Details Using PowerShell
Create a Subfolder Inside an Existing Folder in SharePoint
If you want to create a folder inside another folder (a subfolder) in a SharePoint document library, then you can use the below PnP PowerShell script.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Trainings" -ClientId "your-client-id" -Interactive
# Variables
$parentFolderPath = "/sites/Trainings/Shared Documents/ProjectA"
$subFolderName = "Reports"
Add-PnPFolder -Name $subFolderName -Folder $parentFolderPathAs you can see in the screenshot below, it created the Reports folder under the ProjectA folder in the Shared Documents document library.

Read Add a Choice Column to a SharePoint Online List Using PnP PowerShell
Create Multiple Folders and Subfolders in Bulk using PnP PowerShell
I will show you here how to create multiple folders and subfolders in bulk using PnP PowerShell. When you need to create a predefined folder structure, you can loop through an array of folder names.
Here is the complete PowerShell script.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Trainings" -ClientId "Your Client ID" -Interactive
# Variables
$libraryName = "/sites/Trainings/Shared Documents"
$folderStructure = @(
@{ Name = "ProjectA"; SubFolders = @("Reports", "Invoices", "Contracts") },
@{ Name = "ProjectB"; SubFolders = @("Designs", "Testing", "Deployment") }
)
# Loop and create folders
foreach ($folder in $folderStructure) {
# Create main folder
Add-PnPFolder -Name $folder.Name -Folder $libraryName
# Create subfolders
foreach ($sub in $folder.SubFolders) {
Add-PnPFolder -Name $sub -Folder "$libraryName/$($folder.Name)"
}
}If you execute the code, you can see the message below:

Now, if you open the SharePoint document library, you can see that it has created all the folders and subfolders in the library.

Check out Change a SharePoint Site URL Using PnP PowerShell
Create Folders from a CSV File in SharePoint using PnP PowerShell
Sometimes you might need to create folders from a CSV file in a SharePoint document library.
For this example, I have taken the below csv file (FolderStructure.csv):
MainFolder,SubFolder
ProjectX,Reports
ProjectX,Invoices
ProjectY,Designs
ProjectY,TestingHere is the PnP PowerShell script.
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Trainings" -ClientId "Your Client ID" -Interactive
# Import CSV
$folderData = Import-Csv "C:\Users\fewli\Downloads\FolderStructure.csv"
$libraryName = "/sites/Trainings/Shared Documents"
foreach ($row in $folderData) {
$mainFolderPath = "$libraryName/$($row.MainFolder)"
# Check if main folder exists
$mainExists = Get-PnPFolder -Url $mainFolderPath -ErrorAction SilentlyContinue
if (-not $mainExists) {
# Create main folder
Add-PnPFolder -Name $row.MainFolder -Folder $libraryName
Write-Output "Created main folder: $mainFolderPath"
} else {
Write-Output "Main folder already exists: $mainFolderPath"
}
# Now create subfolder (check existence is optional, usually not needed if subfolder is unique)
Add-PnPFolder -Name $row.SubFolder -Folder $mainFolderPath
Write-Output "Created subfolder: $mainFolderPath/$($row.SubFolder)"
}Once you execute the PowerShell script, you can see the output in the console like below:

Now, if you navigate to the SharePoint document library, you can see the exact output in the screenshot below:

Read Get SharePoint Site ID using PnP PowerShell
Create Nested Subfolders in SharePoint Document Library
Many times, you might need to create nested subfolders in a SharePoint document library using PowerShell. Something like the below deep folder structure:
ProjectA
├── Reports
│ ├── 2025
│ └── 2024
├── Invoices
└── Contracts
To achieve the above folder structure in a SharePoint document library, you can use the following PnP PowerShell script:
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/Trainings" -ClientId "Your Client ID" -Interactive
$libraryName = "Shared Documents" # Just the library name, NOT the site path
$nestedStructure = @{
"ProjectA" = @{
"Reports" = @("2025", "2024")
"Invoices" = @()
"Contracts" = @()
}
}
foreach ($mainFolder in $nestedStructure.Keys) {
$mainFolderPath = "$libraryName/$mainFolder"
$mainExists = Get-PnPFolder -Url "sites/Trainings/$mainFolderPath" -ErrorAction SilentlyContinue
if (-not $mainExists) {
Add-PnPFolder -Name $mainFolder -Folder $libraryName
Write-Output "Created main folder: $mainFolderPath"
} else {
Write-Output "Main folder already exists: $mainFolderPath"
}
foreach ($subFolder in $nestedStructure[$mainFolder].Keys) {
$subFolderPath = "$libraryName/$mainFolder/$subFolder"
$subExists = Get-PnPFolder -Url "sites/Trainings/$subFolderPath" -ErrorAction SilentlyContinue
if (-not $subExists) {
Add-PnPFolder -Name $subFolder -Folder "$libraryName/$mainFolder"
Write-Output "Created subfolder: $subFolderPath"
} else {
Write-Output "Subfolder already exists: $subFolderPath"
}
foreach ($nestedSub in $nestedStructure[$mainFolder][$subFolder]) {
$nestedSubPath = "$libraryName/$mainFolder/$subFolder/$nestedSub"
$nestedExists = Get-PnPFolder -Url "sites/Trainings/$nestedSubPath" -ErrorAction SilentlyContinue
if (-not $nestedExists) {
Add-PnPFolder -Name $nestedSub -Folder "$libraryName/$mainFolder/$subFolder"
Write-Output "Created nested subfolder: $nestedSubPath"
} else {
Write-Output "Nested subfolder already exists: $nestedSubPath"
}
}
}
}Once you run the above PowerShell script, you can see the output as shown in the screenshot below:

Now, when you open the SharePoint document library, you can see the following folder structure:

I hope you now understand how to create folders and subfolders in SharePoint using PnP PowerShell. By using the above PowerShell script to create a single folder, a set of predefined subfolders, or importing a structure from a CSV file using PnP PowerShell. We saw a few real examples with a complete script.
You may also like the following tutorials:
- Get SharePoint Site Creation Date using PnP PowerShell
- Check If a Field Exists in a SharePoint Online List Using PnP PowerShell
- Check if a File Exists in a SharePoint Document Library Using PnP PowerShell
- Get All List Fields in SharePoint Online using PnP PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.