title: Add RoleAssignment Member to SPItem Using PowerShell
body: <p>I will be executing a script to remove permissions from a <code>SPitem</code>. However, a rollback plan is required and I am required to create a separate script which will add the permission of the user back to the <code>SPitem</code> if required.</p>
  
  <p>Below is my code snippet which removes a user from the <code>SPitem</code>:</p>
  
  <pre><code>ForEach ($RDfolderId in $RDfolderSplit)
  {
      $query = New-Object Microsoft.SharePoint.SPQuery
      $query.ViewXml = "@&lt;View Scope='RecursiveAll'&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Title' /&gt;&lt;Value Type='Text'&gt;$RDfolderId&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;"
  
      $RDfolder = $RDlist.GetItems($query)
  
      foreach($role in $RDfolder.RoleAssignments)  
      {
          if ($role.Member.Name.Equals($userToAction))
          {
              #$RDitem.BreakRoleInheritance($true)
              #$RDitem.RoleAssignments.RemoveById($roleAssignment.Member.ID)
              #$RDitem.Update()
          }
      }
  }
  </code></pre>
  
  <p>I have seen code samples online on adding <code>roles</code> back to the <code>SPitem</code>. However, there is an additional field <code>RoleDefinitions</code> declared.</p>
  
  <p>Is it compulsary to have the value declared when adding a user to a <code>SPitem</code>?</p>
  
  <p>Below is the code sample for adding:</p>
  
  <pre><code>$web = Get-SPWeb http://sp-2010
  $account = $web.EnsureUser("SHAREPOINT\mray")
  $role = $web.RoleDefinitions["Contribute"] #is this value compulsory?
  
  $list = $web.Lists["Shared Documents"]
  
  $list.BreakRoleInheritance($true)
  
  $assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
  $assignment.RoleDefinitionBindings.Add($role)
  $list.RoleAssignments.Add($assignment)
  
  $list.Update()
  $web.Dispose()
  </code></pre>
  
  <p><a href="https://social.technet.microsoft.com/Forums/sharepoint/en-US/4373cef1-2471-4199-aa16-784684fc82de/adding-permission-to-list-using-powershell?forum=sharepointadminprevious" rel="nofollow noreferrer">source</a></p>