Copy an Active Directory User With PowerShell

In this blog post, I will show you how I copy an existing Active Directory user and create a new user with PowerShell.

the PowerShell script below, will use a user called user01 as the template and create a user called user02.

During the copy process, the new user will be members of the same groups as user01 and will have the same Active Directory attributes.

Code

#Import module
Import-Module activedirectory
#Set password for new user 
$password = Read-Host -Prompt "Set Password" -AsSecureString
#Copy user - user01
$userInstance = Get-ADUser -Identity "user01" 
#Create a new user from user01
New-ADUser -SAMAccountName "user02"  -Instance $userInstance -DisplayName "User 02" -Name "My User 02" -UserPrincipalName "user02@domain.local" -AccountPassword $password
#Get AD user details of new user
$oupath = Get-aduser user02
$x = $oupath.DistinguishedName
#Move new user to Accounts OU
Move-ADObject -Identity $x -TargetPath "OU=Accounts,DC=domain,DC=local"

 


Posted

in

by