WildBunch
WildBunch

Reputation: 3

PowerShell: Add Users to AD and Add them in Groups

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.

Upvotes: 0

Views: 1515

Answers (2)

websch01ar
websch01ar

Reputation: 2123

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
}

Upvotes: 1

user189198
user189198

Reputation:

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

Upvotes: 2

Related Questions