0

I am trying to assign unique permissions to files in a document library using PowerShell. i.e. iterate through each file in a document library break its role inheritance and assign read permissions to different users for each file.

Below is my powershell script:-

$siteCollUrl = http://mysharepointsite
$LibraryName = "Reports"
$spSourceWeb = Get-SPWeb $siteCollUrl;
$spfiles = $spSourceWeb.getfolder($LibraryName).Files
foreach ($file in $spfiles)
{
  $file.BreakRoleInheritance($true) 

}
$spSourceWeb.dispose();

But I get an error:-

Method invocation failed because [System.String] doesn't contain a method named 'BreakRoleInheritance'.

Any pointers on what's the right way of doing it using powershell?

2

Permissions are on items, and there is a file behind the item. Try this:

$siteCollUrl = http://mysharepointsite
$LibraryName = "Reports"
$spSourceWeb = Get-SPWeb $siteCollUrl;
$list = $spSourceWeb.Lists[$LibraryName]
foreach ($item in $list.Items)
{
  $item.BreakRoleInheritance($true) 
}
$spSourceWeb.Dispose()

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.