0

I have script which is working fine, which creates a new Active Directory user. I need to modify the script to add the user to their security group.

Here is what the contents of my CSV file look like:

Firstname,Lastname,Password,Sam,Group
Alice,Gadbois,azerty+123,a.gadbois,GG1
Quincy,Lagueux,azerty+123,q.lagueux,GG1

and here is my PowerShell script:

$objOU = [ADSI]"LDAP://OU=TestOU,DC=Domain,DC=local";
$dataSource = import-csv -Path "c:\users.csv";

foreach($dataRecord in $datasource) {
    $cn = $dataRecord.FirstName + " " + $dataRecord.LastName
    $sAMAccountName = $dataRecord.Sam
    $givenName = $dataRecord.FirstName
    $Password = $dataRecord.Password
    $sn = $dataRecord.LastName
    $sAMAccountName = $sAMAccountName.ToLower()    
    $displayName = $sn + ", " + $givenName   
    $userPrincipalName = $sAMAccountName + “@domain.local"    
    $objUser = $objOU.Create("user","CN="+$cn)    
    $objUser.Put("sAMAccountName",$sAMAccountName)
    $objUser.Put("userPrincipalName",$userPrincipalName)    
    $objUser.Put("displayName",$displayName)    
    $objUser.Put("givenName",$givenName)    
    $objUser.Put("sn",$sn)    
    $objUser.SetInfo()
    $objUser.psbase.InvokeSet(“AccountDisabled",$false)
    $objUser.SetInfo()
}

I need to add a new command in the script, to add each user to his group.

2 Answers 2

2

Use the ActiveDirectory PowerShell module that's included with the Remote Server Administration Tools (RSAT). It has a command called Add-ADGroupMember.

http://technet.microsoft.com/en-us/library/ee617210.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

Here you go:

As Trevor said, you need to import the Active Directory module at the top of your script.

Import-module ActiveDirectory 

And then within your foreach loop, you can add the Add-ADGroupMember Command.

foreach($dataRecord in $datasource) {
    $cn = $dataRecord.FirstName + " " + $dataRecord.LastName
    $sAMAccountName = $dataRecord.Sam
    $givenName = $dataRecord.FirstName
    $Password = $dataRecord.Password
    $sn = $dataRecord.LastName
    $sAMAccountName = $sAMAccountName.ToLower()    
    $displayName = $sn + ", " + $givenName   
    $userPrincipalName = $sAMAccountName + “@domain.local"    
    $objUser = $objOU.Create("user","CN="+$cn)    
    $objUser.Put("sAMAccountName",$sAMAccountName)
    $objUser.Put("userPrincipalName",$userPrincipalName)    
    $objUser.Put("displayName",$displayName)    
    $objUser.Put("givenName",$givenName)    
    $objUser.Put("sn",$sn)    
    $objUser.SetInfo()
    $objUser.psbase.InvokeSet(“AccountDisabled",$false)
    $objUser.SetInfo()

    Add-ADGroupMember -Identity $dataRecord.Group -Member $sAMAccountName
} 

Troubleshooting

Verify that each user has group properly assigned:

$users = Import-Csv "Path_To_File.csv"
$users | % {
    $_.Group
}

21 Comments

Thanks, but it doesnt work it add user to group, i need to add group to user. The Cmdles you gave me right now if i use it i will add the same group to all users in my CSV file.
websch01ar are You there ?
My apologies. To make it easier, I have updated my post.
Users are there bu there only members of one groups which is domain users. I need them to be add into diferent groups which are in the csv file. Thanks
Can you look at the data in the CSV file? You are iterating over each record so $dataRecord.Group is distinct for each row. However, if each row has the same value, then it will be the same.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.