Create Libraries/Folders/Documents in SharePoint Online using PowerShell PnP
Today, we’ll look at creating documents libraries & folders, then upload documents into those folders using PowerShell PnP.
For more info about the Practices & Patterns (PnP), refer to the official documentation, and/or download the modules from hard workers in GitHub – SharePoint PowerShell PnP.
Connect to the SPO Site Collection
First things first, let’s connect to the SharePoint site collection where we want to create our document library.
Connect-PnPOnline -Url https://<TenantName>.sharepoint.com/sites/pnptest -Credential (Get-credential)
Create a new document library
Now that we’re logged into the site collection, let’s create our document library.
New-PnPList -Title "PnP Library" -Template DocumentLibrary -OnQuickLaunch
We’ve got more parameters if we want (i.e.: Enable versioning, enable content types, etc…). Just try them out with what you need.
If you use the PowerShell ISE with Intellisense, you’ll see a list of templates showing up to help you!
If you wish to verify the document library is there, use the Get-PnPList cmdlet.
Create a folder in “PnP Library”
Going down in the hierarchy, let’s create a folder in that new “PnP Library“.
The parameter “-Folder” refers to the relative URL.
Add-PnPFolder -Name "PnP Folder1" -Folder "/PnP Library"
Let’s verify the folder is there with PowerShell with the Get-PnPListItem cmdlet.
Get-PnPListItem -List "/PnP Library/PnP Folder1"
Upload documents to the folder
Last but not least, uploading documents to the “PnP Folder1”
Add-PnPFile -Folder "/PnP Library/PnP Folder1" -Path 'C:\Users\$env:USERNAME\Desktop\TestPnP.docx'
And for fun… Let’s upload another one…
There you have it!