Home > OS >  Copy groups from one user to another in Active Directory
Copy groups from one user to another in Active Directory

Time:08-31

I'm trying to run this PowerShell script that I found but it's not working for me and I'm getting an error could someone else check it and tell me if there is an issue here?

   $CopyFromUser = Get-ADUser userName -Server domainName -Properties MemberOf
$CopyToUser = Get-ADUser userName -Server domainName -Properties MemberOf
$CopyFromUser.MemberOf | Where{$CopyToUser.MemberOf -notcontains $_} | Add-ADGroupMember -Members $CopyToUser
pause

This is the error that I'm getting.

| A positional parameter cannot be found that accepts argument
 | 'Microsoft.ActiveDirectory.Management.ADPropertyValueCollection'.

CodePudding user response:

Add the group memberships one at a time, explicitly pass the target DN to the -Identity parameter:

$CopyFromUser = Get-ADUser userName -Server domainName -Properties MemberOf
$CopyToUser = Get-ADUser userName -Server domainName -Properties MemberOf
$CopyFromUser.MemberOf |Where-Object {$CopyToUser.MemberOf -notcontains $_} |ForEach-Object {
    Add-ADGroupMember -Identity $_ -Members $CopyToUser
}
  • Related