Reputation: 87
I have code to import users and modify their properties:
Import-Csv users.csv |
select Surname, @{n='GivenName';e={$_.'FirstName'}},
@{n='samaccountname';e={$_.FirstName.substring(0,2) + $_.Surname}},
@{n='UserPrincipalName';e={$_.FirstName.substring(0,2) + $_.Surname}},
@{n='Name';e={$_.'FirstName' + ' ' + $_.'Surname'}} |
New-ADUser -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText "Password1" -Force) -ChangePasswordAtLogon $true -Path "OU=Intake 20XX,OU=Students,OU=Ravenloft users,DC=RAVENLOFT,DC=test" -ProfilePath {"\\TESTSVR\Profiles$\Intake20XX\" +$_.SamAccountName} -HomeDrive "D:" -HomeDirectory {"\\TESTSVR\Work$\Intake20XX\" +$_.SamAccountName} -PassThru |
Foreach-Object {
Add-ADGroupMember -Identity "CN=StudentGroup,OU=Students,OU=Ravenloft users,DC=RAVENLOFT,DC=test" -Members $_
}
What I'm looking to do is add them to a second group in addition to the "Studentgroup" that is already working.
Upvotes: 0
Views: 34
Reputation: 200573
Simply do another Add-ADGroupMember
:
Foreach-Object {
Add-ADGroupMember -Identity 'StudentGroup' -Members $_
Add-ADGroupMember -Identity 'othergroup' -Members $_
}
You can use the SamAccountName
of the groups, BTW. The distinguished name is not required.
With that said, if you want to add all new users to the same group, it would be better to collect the users in a variable, and then add them all in one go:
$users = Import-Csv users.csv | ... | New-ADUser ... -PassThru
Add-ADGroupMember -Identity 'StudentGroup' -Members $users
Add-ADGroupMember -Identity 'othergroup' -Members $users
Upvotes: 1