Creating SharePoint Document Sets with PowerShell

Document Sets are great for scenarios where you need to store documents together and tag them with common metadata. I often use Document Sets for managing client documents, contracts management and job folders.

This example script uses PnP PowerShell to read a CSV file and then create Document Sets of a specific Content Type in the specified Document Library. The script creates a parent folder based on the Client Type with the Document Set inside the folder.

You will need to do the following configuration before running the script:

  • Go to Site Settings > Site Collection Features and enable Document Sets
  • Go to the Content Type Gallery and create a new Document Set Content Type. Add any site columns needed.
  • Go to Document Library Settings > Advanced and enable Content Types. Add the Document Set content type you created above to the library.

To connect with PnP PowerShell you will need to do an Azure App Registration and use the Client ID value in the Connect-PnPOnline command to authenticate.

Connect-PnPOnline -URL https://tenantname.sharepoint.com/sites/TestSite -ClientID xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx -Interactive

$Library = "DocSetTesting"
$DocSetCT = "ClientDocSet"

$ClientDocSets = Import-CSV c:\temp\docsettestdata.csv

foreach($Client in $ClientDocSets)
{
	$DocSetName = $Client.ClientID+"-"+$Client.ClientName
	write-host "Creating.." $DocSetName
	$FolderPath = $Library+"/"+$Client.ClientType
	$NewDocSet = Add-PnPDocumentSet -List $Library -ContentType $DocSetCT -Name $DocSetName -Folder $FolderPath
	$DocSet = Get-PnPFolder -Url $NewDocSet -AsListItem 
	Set-PnPListItem -List $Library -Identity $DocSet.ID -Values @{"ClientID" = $Client.ClientID}
}

CSV file must have a header row with the property names e.g. ClientName, ClientID, ClientType. The values in my example are all text, but you can use dates, numbers etc.

Here are a few things you might want to customise in the script:

  • Add-PnPDocumentSet – remove the “-Folder” option if you don’t want a sub folder.
  • Set-PnPListItem – add additional metadata values (don’t forget to include them in the CSV file)

There you have it, a nice easy way to bulk create Document Sets and assign metadata. Any documents saved into the Document Sets will inherit the metadata fields you have configured to share in the Document Set configuration.

Learn more about Document Sets.


Discover more from SharePoint Moments

Subscribe to get the latest posts sent to your email.

Leave a comment