Create a folder structure in SharePoint using PowerShell (from file shares)
Recently, I had to create a SharePoint folder structure extracted from a file share. I only wanted the structure, and not the files.
So I had the local part to extract what I needed, and then I thought “sure I’ll use PowerShell PnP with a foreach loop, and done!” Well, surprisingly (or not), that didn’t work as expected 😟 So after a few hours trying and trying again, I decided to ask for help… (Never be afraid to ask for help, because you’ll 100% learn something!)
In this blog post, I’m going to share the script that Adam Listek (Twitter: @alistek) made within a few minutes, and help me tremendously (I have his permission for sharing). Thanks Adam!
Exctract structure from File Shares
The below one-liner allowed me to export the root folder as well as all nested folders like I needed.
So… I’m using… :
Recurse parameter to get the nested folders
Directory parameter to get only the folders (not the files) FullName property will give me the full path
NoTypeInformation to avoid too much info at the top of the excel file… Now that we have the original structure, let’s modify it a little bit.
Ctrl+F on the keyboard,
Enter the location for the root folder (C:\Github\ in my case),
This is what we need:
Create the structure in SharePoint Online
Now’s the time to make it like a breeze… I’ll spare you my hair pulling theories, which were too complex when simplicity should have been thought through…
I will however, explain the logic and requirements behind it. On this extract above, we have folders alone (i.e.: Dev-SPFarm-2016), and we have nested folders (i.e.: vPS365\docs)
Folders alone are not such a big deal to create. But nested folders… they need the ‘root’ to be created before right? 😉
So here we go. This is what we need to do: (change to YOUR TENANT NAME, SITE, and DOC LIBRARY)
- Connect to the site collection (where the library is)
- Declare the structure
- If not too long, it’s easy to insert in the script.
- If too long, just replace by
Import-Csv -Path <Your_Csv_file>
- Declare the document library (which you’ll find in the URL – not the display name with any space you may have created it!)
- And then we start creating the structure.
- Set the
$i = 0
- Loop through each item,
- Condition if ‘root’ or subfolder
- Set the
‘SilentlyContinue’if you don’t want to see red when the ‘root’ folder is already created
- A bit of artefacts with
- Use the
Add-PnPFoldercmdlet for creation
$i++to continue as long as we have folders to create!
- Set the
Script for MacOS users
Script for Windows users
Thanks for reading!