AD User Group Copy

This Script was designed for a Service Desk Analyst to run. The AD user was already created and required the same AD groups as another user in the domain. This script exports the groups and adds them to the other user.

NOTE – This script does not overwrite existing groups. 

Below is an index of the script sections to explain what each section does.

  1. Imports the AD powershell module
  2. Ask the initiator Source and destination usernames
  3. List the source user groups in Black text, grey background
  4. Add the source groups to the destination user, silently continue on errors (it will error when a group exists already)
  5. List the destination user groups after the copy in Black text, green background
## 1 Import AD module
Import-Module ActiveDirectory

## 2 Gather usernames from intiator
$srcuser = Read-Host "Username to copy groups from ->"
$destuser = Read-Host "Username to copy groups to ->"

## 3 List Source Groups
$srcgroups = (Get-ADPrincipalGroupMembership -Identity $srcuser | Select-Object -ExpandProperty name) -join ';'
Write-Host "Source User Groups are the following:" -ForegroundColor Black -BackgroundColor Yellow
Write-Host $srcgroups -ForegroundColor Black -BackgroundColor Gray

## 4 Get source groups and add them to the destination user
Write-Host "Group Copying Has Started" -ForegroundColor Black -BackgroundColor Yellow
Get-ADUser -Identity $srcuser -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $destuser -ErrorAction SilentlyContinue
Write-Host "Group Copying Has Finished" -ForegroundColor Black -BackgroundColor Yellow
sleep -Seconds 5

## 5 List Destination Groups
$destgroups = (Get-ADPrincipalGroupMembership -Identity $destuser | Select-Object -ExpandProperty name) -join ';'
Write-Host "Destination User Groups are now the following:" -ForegroundColor Black -BackgroundColor Yellow
Write-Host $destgroups -ForegroundColor Black -BackgroundColor Green

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.