0

I am trying to remove all the permissions from folders in sharepoint using a powershell script but only some of the permissions are being removed. When I manually run the powershell commands I can see that there are four role assignmebers but only two are removed.

PS C:\support\scripts> $rvn.Item.RoleAssignments.Count
4
PS C:\support\scripts> for($i=0;$i -lt $rvn.Item.RoleAssignments.Count; $i++){
>> $ra = $rvn.Item.RoleAssignments[$i]
>> write-host "$i $($ra.Member.LoginName)"
>> }
>>
0 Microsoft CRM Visitors
1 rmvd\timc
2 rmvd\amyw
3 rmvd\AaronR
PS C:\support\scripts> for($i=0;$i -lt $rvn.Item.RoleAssignments.Count; $i++){
>> $ra = $rvn.Item.RoleAssignments[$i]
>> write-host "$i $($ra.Member.LoginName)"
>> $rvn.Item.RoleAssignments.Remove($ra.Member)
>> }
>>
0 Microsoft CRM Visitors
1 rmvd\amyw
PS C:\support\scripts>
1

You remove Microsoft CRM Visitors, and rmvd\timc becomes RoleAssignments[0]. So in the next iteration you remove RoleAssignments[1] which is now rmvd\amyw. RoleAssignments[2] is now empty.

You need to either always remove the last entry, or do this using a while loop.

  • That makes sense. I did not think about the fact that I was modifying the collection I was looping over. Thank you – Gabaroar Jan 2 '15 at 20:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.