locked
How to manage permissions of files in SharePoint sites using PowerShell? RRS feed

  • Question

  • I want to manage the permissions of files (tens of thousands in number) kept in different folders of a SharePoint site in my Office 365 admin account. How to achieve the same using PowerShell script.

    My SharePoint site URL is like https://xxxxx.sharepoint.com/sites/yyyyy

    I am trying to access https://xxxxx-admin.sharepoint.com/ using the real credentials.

    Actually, I am able to log in to the Office 365 account. But I do not know how to access folders and files inside the SharePoint sites and change their permissions. 

    Can anyone help to achieve this? Thanks.

    function Set-SPOListResetRoleInheritance
    {  
        param (
            [Parameter(Mandatory=$true,Position=1)]
            [string]$Username,
            [Parameter(Mandatory=$true,Position=2)]
            [string]$Url,
            [Parameter(Mandatory=$true,Position=3)]
            [string]$AdminPassword,
            [Parameter(Mandatory=$false,Position=4)]
            [bool]$IncludeSubsites=$false
        )
    
    
    
        $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
        $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
        $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
    
        $web = $ctx.Web
        $ctx.Load($web)
    
        $lists = $ctx.Web.Lists
        $ctx.Load($lists)
    
        $webs = $ctx.Web.Webs
        $ctx.Load($webs)
    
        $ctx.ExecuteQuery()
    
        Write-Host 
        Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
    
        foreach( $ll in $lists)
        {            
            $ll.ResetRoleInheritance()
            $ll.Update()
    
            try
            {
                $ctx.ExecuteQuery()
                Write-Host "Restored inherited permissions for " $ll.Title
            }
            catch
            {
                Write-Host "Failed to restore permissions for " $ll.Title
    
            }
        }
    
        if($ctx.Web.Webs.Count -gt 0 -and $IncludeSubsites)
        {
            for($i=0; $i -lt $ctx.Web.Webs.Count ; $i++)
            {
                Set-SPOListResetRoleInheritance -Url ($ctx.Web.Webs[$i].Url) -Username $Username -AdminPassword $AdminPassword -IncludeSubsites $IncludeSubsites
            }
        }
    }
    
    
    Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll"
    
    
    Set-SPOListResetRoleInheritance -Username "sai@xxxxxxxx.com" -Url "https://xxxxxxxx-admin.sharepoint.com" -AdminPassword "xxxxxxxx" -IncludeSubsites $true




    Friday, June 29, 2018 11:03 AM

All replies