I have a document libray with structure like given below: FolderA->Subfolder1->item1 Subfolder2-->item2 They have folder level permissions . now i want to create the same folder structure and permissions with different Folder Parent name for example:

Once we copy the folder structure should be FOLDER B-->Subfolder1->item1 Subfolder2-->item2

This is for sharepointonline CSOM powershell.

I was able to copy the folder structure but unable to get permissions is anything missing

    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.dll" 
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$admin = ''
$password = ConvertTo-SecureString "sdsd" -AsPlainText -Force

Function Copy-SPOFolder([String]$SiteURL, [String]$SourceFolderURL, [String]$TargetFolderURL)
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Copy the Folder
        $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
        [Microsoft.SharePoint.Client.MoveCopyUtil]::CopyFolder($Ctx, $SourceFolderURL, $TargetFolderURL, $MoveCopyOpt)

        Write-host -f Green "Folder Copied Successfully!"
    Catch {
    write-host -f Red "Error Copying the Folder!" $_.Exception.Message

Try {

        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin,$password)
        $Ctx.Credentials = $credentials
        $Web = $Ctx.web
         $Folders = $Library.RootFolder.Folders
   #Get existing folder names
        $FolderNames = $Folders | Select -ExpandProperty Name
        if($FolderNames -contains $NewFolderName)
            write-host "Folder Exists Already!" -ForegroundColor Yellow


                   $SourceFolderURL= $SiteURL + $LibraryName +"/" + $SrcFolderName
                    $TargetFolderURL= $SiteURL + $LibraryName +"/" + $NewFolderName 

                    Copy-SPOFolder $SiteURL $SourceFolderURL $TargetFolderURL



Catch {
        write-host -f Red "Error Granting permission to  Folder!" $_.Exception.Message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.