P S
asked on
Need a powershell script to add multiple users to multiple groups
I've a CSV file with information something like below.
samaccountname Groupname
Test1 Group1
Test2 Group2
Test3 Group3
. .
. .
TestN Group N
I've a task to add all users in each of the groups. There could be some usernames which are not part of Active directory. I need a script which should add user's in the groups and those who are not part of AD, an output CSV file should be generated which highlights the account which were not found in AD.
Thanks.
samaccountname Groupname
Test1 Group1
Test2 Group2
Test3 Group3
. .
. .
TestN Group N
I've a task to add all users in each of the groups. There could be some usernames which are not part of Active directory. I need a script which should add user's in the groups and those who are not part of AD, an output CSV file should be generated which highlights the account which were not found in AD.
Thanks.
I believe you want to add each listed user in csv to all groups in csv, right?
I mean there is no mapping required between each user and corresponding group in csv file ?
I mean there is no mapping required between each user and corresponding group in csv file ?
If you want user1 to be added to all groups (group1 to GroupN), then I would split the CSV file into two files. A list of users and a list of groups.
Then do a nested for next loop, the outer one to process a group at a time, and an inner loop to process all the users for that group and add the user to the group.
If you want me to update the code, let me know.
Then do a nested for next loop, the outer one to process a group at a time, and an inner loop to process all the users for that group and add the user to the group.
If you want me to update the code, let me know.
ASKER
Mahesh,
This is what i am expecting from the script.
Samaccountname Group
User1 Group1
User2 Group2
User3 Group3
User4 Group4
. .
. .
. .
User N Group N
User 1 should get added in all the groups i.e from Group1 till Group N and same goes to all other users as well. Basically, every user should be added in all the groups.
Let me know if any other info is required.
Thanks
This is what i am expecting from the script.
Samaccountname Group
User1 Group1
User2 Group2
User3 Group3
User4 Group4
. .
. .
. .
User N Group N
User 1 should get added in all the groups i.e from Group1 till Group N and same goes to all other users as well. Basically, every user should be added in all the groups.
Let me know if any other info is required.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Mahesh. It worked!!
# Add users to groups
Param ($infile)
$userlist = Import-CSV -Path $infile
$stream = [System.IO.StreamWriter] "users_not_found.txt"
foreach ($user in $userlist)
{
$userid = $user.samAccountName
$userobj = Get-ADUser $userid
if ($userid) {
$group = $user.Groupname
Add-ADGroupMember -Identity $group -Members $userid
}
else {
write-output "$userid not found"
$stream.WriteLine("$userid
}
}
$stream.Close()