Creating an AD User from a Template in Powershell

Hi,

I have a VM where I’m practicing Powershell. I have been able to successfully create new active directory users and also templates.
However when it comes to creating an AD User based of a template via powershell , I’m not able to accomplish that. Will someone please assist or point me in the right direction.

For example, this is what I tried doing.

I have created a Template called IT Template, and that IT template is part of different groups(Members of tab)
When I create new users, I want it to use the properties of the IT Template so I don’t have to add all of the groups manually.

I know I can add it Manually, like this.
add-adgroupmember -Identity “Office” -members “test user”
This will add the Office group to the user called test user.
However I want to attach this to the template level.

Can you provide me with a basic example so I can start understanding this.

Thanks

Regards,
Jasiel

retrieve the ad groups your template user is a member of

$groupstoadd = get-adprincipalgroupmembership templateidhere

then loop through the groups and add to your user

[quote quote=260011]retrieve the ad groups your template user is a member of

$groupstoadd = get-adprincipalgroupmembership templateidhere

then loop through the groups and add to your user

[/quote]

Thanks, can you show me in a small example how to achieve this. I can’t wrap my head around it.

[pre]
$groupstoadd = get-adprincipalgroupmembership templateidhere

foreach ($group in $groupstoadd)
{
add-adgroupmember -identity $group -members $newuserobjectvariable
}
[/pre]

I’m not sure where the code would be. Here is what I have.

Import-Module activedirectory
$ADusers =Import-csv c:\bulkusercreation.csv
foreach ($user in $ADUsers)
{
$Username = $User.username
$Password = $User.password
$Firstname =$User.firstname
$LastName = $User.lastname
$email = $User.email
$company = $User.company
$description = $User.description

Then go into checking if the user exist, if not then it goes into creating the new user. 
THis is where I'm stuck. I'm still not sure where in the code , or how do I code the create from template. 
Can you show me where or point me in the right direction? 

so the values you’re inputting there are distinct to the new user.

if you want to use values from the template user for any other ad attributes you will need to retrieve it.

$template = get-aduser templateidhere

you will need to add any properties you want to copy to the new user utilizing the -properties argument.

the $template line can be added after line 2.

(this of course is assuming your “template” is an actual ad user object you’ve built)

for the group membership, you need to perform that action after your new-aduser command builds your new user object