title: Copy group membership from one user to another in AD
body: <p>Im tyring to build a script which will copy group memberships from one user to another in AD. Im trying to use powershell to automate this task. However im stuck while creating a check for the user. In other words when i copy group membership from one user to another i want to be able to run a check to see if the user is already a member of the group before adding them, bu doing this i can avoid errors which such as " this user is already a member of the group and cannot be added again" Any help or advice would be appreciated. Im using the following to script at the moment. </p>
  
  <pre><code>$copy = Read-host "Enter user to copy from"
  $Sam  = Read-host " Enter user to copy to"
   Function Copymembership {
  
  $members = Get-ADUser -Identity $copyp -Properties memberof
  foreach ($groups in $members.memberof){
  if ($members -notcontains $groups.sAMAccountname)
  {Add-ADGroupMember -Identity $groups -Member $sam -ErrorAction SilentlyContinue
  Write-Output $groups} 
  }
  }
  copymembership 
  </code></pre>