Create AD Accounts and Add To Group

So this is working as far as creating accounts and putting them in the proper OU. However I can’t figure out how to add those users to the group “All-Students”.

Here is the line in question:

Add-ADGroupMember -Identity All-Students -Member $SAM 

Screenshot of the excel file we convert: http://i.imgur.com/sadEQQt.png

Any ideas? Thanks!

Full script:

$date = Get-Date #Set up Log files for output $ErrorLog = "C:TempAddStudentssErrorlog.txt" $SuccessLog = "C:TempAddStudentsSuccesslog.txt" Add-Content $SuccessLog "-----------------------------------------------------------------" Add-Content $SuccessLog $date Add-Content $SuccessLog "-----------------------------------------------------------------" Add-Content $ErrorLog "-------------------------------------------------------------------" Add-Content $ErrorLog $date Add-Content $ErrorLog "-------------------------------------------------------------------" ## Add AD Cmdlets Import-Module ActiveDirectory $UserList = Import-Csv -Delimiter "," -Path "C:Tempteststudentgroup.csv" foreach ($User in $UserList) { $Displayname = $User.'Firstname' + ' ' + $User.'Lastname' $UserFirstname = $User.'Firstname' $UserLastname = $User.'Lastname' $OU = $User.'OU' $SAM = $User.'SAM' $UPN = $User.'SAM' + "@" + $User.'Maildomain' $Description = $User.'Description' $Password = $User.'Password' $HomeDirectory = $User.'HomeDirectory' $HomeDrive = $User.'HomeDrive' #Check if the User exists New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true -Enabled $true -server dc-1.xxxxxxxxxxxx -HomeDirectory $HomeDirectory -HomeDrive $HomeDrive Add-ADGroupMember -Identity All-Students -Member $SAM Write-Host "AD Account $Displayname created!" add-content $SuccessLog "User $Displayname created Sucessfully." Write-Host -ForegroundColor Green $Displayname 

submitted by /u/Fiala06
[link] [comments]

Leave a Reply