Documentset is a hidden gem in SharePoint, it’s a folder with metadata.
This feature can be activated in the site collection features and will add the “Document Set” Contenttype to the site.
In a recent project I had to mass create Documentsets with predefined metadata.

This article is not about how fabulous Documentsets is, it’s about how to mass create Documentsets with PnP PowerShell.
Fist thing first, create a new site content type based on “Document Set”

Add some metadata to your new Contenttype

Add the Documentset to your Document library

When in use, the Documentset will have a different icon than regular folders, an we can assign metadata to the Documentset itself.

The below code is a small portion of a working script to showcase how it can be done. Dependent on your business case, you will need to modify and extend the script to do whatever you need it to do.
#Connect to SharePoint online site
$spcon = Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/site" -Interactive -ReturnConnection
#new docset metadata
$doclibrary = "Shared Documents"
$docsetName = "Documentset 1002"
$docsetContenttype = "My Documentset"
$Year = "1002"
$ResponsiblePerson = "UPN" #UPN of the user
$ResponsibleUnit = "Marketing"
#check if the Documentset exist or not
$docsetexist = Get-PnPFolder -Url "https://tenant.sharepoint.com/sites/sites/$doclibrary/$docsetName" -Connection $spcon -ErrorAction SilentlyContinue
if($docsetexist)
{
#write-host
Write-Host "Documentset exist in library: " $docsetName
}
else
{
#Add-PnP Documentset, then update documentset metadata
Write-Host "Creating Documentset: " $docsetName -BackgroundColor Green
$docset = Add-PnPDocumentSet -List $doclibrary -ContentType $docsetContenttype -Name $docsetName -Connection $spcon
$docsetprop = Get-PnPFolder -Url $docset -Connection $spcon
$docsetprop.Context.Load($docsetprop.ListItemAllFields)
$docsetprop.Context.ExecuteQuery()
#Set docset metadata
Set-PnPListItem -List $doclibrary -Identity $docsetprop.ListItemAllFields.Id -Values @{Year="$Year"; Responsible_x0020_Person="$ResponsiblePerson"; Responsible_x0020_Unit="$ResponsibleUnit"} -Connection $spcon
}
Change the metadata in the script and run it, the results in SharePoint Online is pretty cool.
