SharePoint Online: Create Document Set using PowerShell

Requirement: Create a Document Set in SharePoint Online using PowerShell

SharePoint Online: Create Document Set using PowerShell
While my another post explains how to create document set in SharePoint Online: Creating Document Set in SharePoint Online - Step by Step, here is the PowerShell script to create document sets in SharePoint Online. This script assumes that the document sets feature is enabled, Content type for document set is created and added to the library already.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.DocumentManagement.dll"

$DocumentLibraryName = "Proposal Documents"
$DocumentSetName = "Crescent Inc SharePoint"
#Content Type ID from the List!

#Get Credentials to connect
$Cred= Get-Credential
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the Artifacts

    #Get content type from library by ID
    $ContentType = $DocumentLibrary.ContentTypes.GetById($ContentTypeID)
    If($ContentType -ne $Null)
        # sharepoint online powershell create document set from content type
        $DocumentSet = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Ctx,$DocumentLibrary.RootFolder,$DocumentSetName,$ContentType.ID)

        Write-host "Document Set '$DocumentSetName' Created Successfully!'" -f Green
        Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
Catch {
    write-host -f Red "Error:" $_.Exception.Message
This creates a new document set in SharePoint Online.
sharepoint online create document set powershell

PnP PowerShell to Create a Document Set in SharePoint Online
Here is how to create document set in SharePoint Online using PnP PowerShell:
$SiteURL= ""
$ListName = "Documents"
$ContentTypeName = "Invoice Doc Set"
$DocumentSetName = "Crescent Invoices"

#Connect to the Site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Create a Content Type from "Document Set" Parent content type
$ParentCType = Get-PnPContentType -Identity "Document Set"
$ContentType = Add-PnPContentType -Name $ContentTypeName -Group "Crescent" -ParentContentType $ParentCType

#Add the Content Type to Library
Add-PnPContentTypeToList -List $ListName -ContentType $ContentType

#Add Document Set to Library
Add-PnPDocumentSet -List $ListName -ContentType $ContentTypeName -Name $DocumentSetName


  1. Hi Salaudeen,

    i need to create Document set inside subfolder (e.g i want to move a file XYZ.doc in a document set inside Test Library, document set name should be file name if document set not exist it should create and then move the file into it )
    Thanks in Advance

  2. Hi

    On this line

    $DocumentSet = [Microsoft.SharePoint.Client.DocumentSet.DocumentSet]::Create($Ctx,$DocumentLibrary.RootFolder,$DocumentSetName,$ContentType.ID)

    I get the error

    Cannot convert the "Microsoft.SharePoint.Client.ClientContext" value of type "Microsoft.SharePoint.Client.ClientContext" to type "Microsoft.S

    Any ideas?


    1. Uninstall the previous version of SharePoint Online Client Components SDK & SharePoint Online Management Shell (If installed), Download and install the latest version!

  3. Where did you find this information ?


    1. Thanks it work.

      Howerver I have 2 remaining problems:

      1st : It create it at the root of the Library. I want it to be created in a subfolder but I don't understand how to add subfolder to the Folder parameters. Can you help me ?

      2nd : This document set have many metadata column which I have info in xls file. is it possible to create the documentset and then "push" the data in the column with the same script ?

      Thanks in advance,

    2. Build up a $parentFolder and pass it instead of $DocumentLibrary.RootFolder

      $parentFolder = Get-PnPFolderItem -FolderSiteRelativeUrl "Test AFV Library/01 Level" -ItemName "02 Level" -ItemType Folder
      write-host parentFolder.Name: $parentFolder.Name

  4. I keep getting the error message -

    Error: Cannot convert argument "ctid", with value: "0x0120D520007C13A4BFEC96904182906261A074F784", for "Create" to type "Microsoft.SharePoint.Client.ContentTypeId": "Cannot convert the "0x0120D520007C13A4BFEC96904182906261A074F784" value of type "System.String" to type "Microsoft.SharePoint.Client.ContentTypeId"."

    I know the ctID is correct. I have checked it again and again. Any ideas?

    1. I have resolved this issue now. No further support/action is needed.

  5. I now have a different issue. I want to include the document set column values (custom columns assigned tot he content type created for the document set) in the creation process. I have tried using a Hashtable containing the docset properties. Afterwards, I simply added an argument to the end of the DocumentSet.Create method which references the hashtable as a fifth argument (I had previously found other examples of this on the internet). Is this the correct process? Now I get an error message - Error: Cannot find an overload for "Create" and the argument count: "5". If I look at the described syntax for the DocumentSet.Create method, it mentions no first argument as being needed to specify the SP Client Context (which is what we are doing in the above example). How can I troubleshoot this? Thank-you.


Please Login and comment to get your questions answered!

Powered by Blogger.