Powershell PNP script doesn't seem to run at all.

Anonymous
2023-05-25T00:40:45.2766667+00:00

Hello,

I have been looking for a way to bulk-delete unique permissions in my SPO site, I found through the SharePoint community a pnp script that I ran through PS7, I do not receive any errors. it just returns the prompt. I check the items in the SharePoint library and nothing has changed so I no it's not working, but without any errors I don't know what isn't working. Below is the script I'm running . . . Anyone have any idea where I'm going wrong here?

#Set Variables
$SiteURL = "https://brookfield.sharepoint.com/sites/teams/BPGHotel"
$FolderURL = "/Shared Documents" #Document Library Site Relative URL
 
#Connect to PnP Online
Connect-PnPOnline -Url https://TENANT-admin.sharepoint.com -Interactive
 
#Function to reset permissions of all Sub-Folders
Function Reset-SubFolderPermissions($FolderURL)
{
    #Get all sub-folders of the Folder - Exclude system folders
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"}
 
    #Loop through each sub-folder
    ForEach($SubFolder in $SubFolders)
    {
        $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name
        Write-host -ForegroundColor Green "Processing Folder '$($SubFolder.Name)' at $SubFolderURL"
 
        #Get the Folder Object - with HasUniqueAssignments and ParentList properties
        $Folder = Get-PnPFolder -Url $SubFolderURL -Includes ListItemAllFields.HasUniqueRoleAssignments, ListItemAllFields.ParentList, ListItemAllFields.ID
 
        #Get the List Item of the Folder
        $FolderItem = $Folder.ListItemAllFields
 
        #Check if the Folder has unique permissions
        If($FolderItem.HasUniqueRoleAssignments)
        {
            #Reset permission inheritance
            Set-PnPListItemPermission -List $FolderItem.ParentList -Identity $FolderItem.ID -InheritPermissions
            Write-host "`tUnique Permissions are removed from the Folder!"
        }
 
        #Call the function recursively
        Reset-SubFolderPermissions $SubFolderURL
    }
}
   
#Call the function
Reset-SubFolderPermissions $FolderURL	
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,251 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-05-25T06:51:37.6366667+00:00

    Hi @Anonymous

    Per my test, you need to connect to sharepoint site instead of admin center. Please refer to following script

    #Set Variables
    $SiteURL = "https://xxx.sharepoint.com/sites/abc"
    $FolderURL = "/TestLibrary9" #Document Library Site Relative URL
     
    #Connect to PnP Online
    $UserName="xx@xxx.onmicrosoft.com"
    $Password = "xxxxxxx"
     
    $SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword
     
    #connect to sharepoint online site using powershell
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Function to reset permissions of all Sub-Folders
    Function Reset-SubFolderPermissions($FolderURL)
    {
        #Get all sub-folders of the Folder - Exclude system folders
        $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder | Where {$_.Name -ne "Forms" -and $_.Name -ne "Document"}
     
        #Loop through each sub-folder
        ForEach($SubFolder in $SubFolders)
        {
            $SubFolderURL = $FolderUrl+"/"+$SubFolder.Name
            Write-host -ForegroundColor Green "Processing Folder '$($SubFolder.Name)' at $SubFolderURL"
     
            #Get the Folder Object - with HasUniqueAssignments and ParentList properties
            $Folder = Get-PnPFolder -Url $SubFolderURL -Includes ListItemAllFields.HasUniqueRoleAssignments, ListItemAllFields.ParentList, ListItemAllFields.ID
     
            #Get the List Item of the Folder
            $FolderItem = $Folder.ListItemAllFields
     
            #Check if the Folder has unique permissions
            If($FolderItem.HasUniqueRoleAssignments)
            {
                #Reset permission inheritance
                Set-PnPListItemPermission -List $FolderItem.ParentList -Identity $FolderItem.ID -InheritPermissions
                Write-host "`tUnique Permissions are removed from the Folder!"
            }
     
            #Call the function recursively
            Reset-SubFolderPermissions $SubFolderURL
        }
    }
       
    #Call the function
    Reset-SubFolderPermissions $FolderURL	
    
    
    
    

    Here is the test result

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful