Add multiple users to multiple AD-groups
I got a question from the support at a customer how they could add multiple users to multiple groups without user the mmc “users and computers”, this is my quick solution…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<# .Synopsis Combine two txt-files to one csv-file to import multi-user to multi-groups in AD .DESCRIPTION This script can be used to import x-numbers of users to x-numbers of ad-groups .EXAMPLE The script is based on three files... - users.txt - a txt-file with users samaccountname - groups.txt - a txt-file with the groups which the users should be member of .EXAMPLE Another example of how to use this workflow .NOTES ************************************************************** Created: 2019-05-08 Version: 1.0 Disclaimer: This script is provided "AS IS" with no warranties Author - Christian Damberg Twitter: @DambergC Blog : https://www.damberg.org ************************************************************** #> #step one - read users.txt file $users = get-content .\users.txt -Encoding UTF8 #step two - this loop will run for all users in txt-file "users.txt" foreach ($user in $users) { #step three - this loop run all groups in "groups.txt" for each user $rk1_group = Get-Content .\groups.txt -Encoding UTF8 foreach ($group in $groups) { #step four - merge both txt-files to "result.txt" "$($user);$group" | Out-File .\result.txt -Append utf8 } } #Step five - import the txt-file "result.txt" and export it to a csv-file named "user_group.csv" import-csv .\result.txt -Delimiter ";" -Header user,group | export-csv .\user_group.csv -NoClobber -Encoding UTF8 #Step six - Read csv-file och run a loop with add-adgroupmember to add each user to each group. #jobbet att lägga till, läs in csv och kör en lopp på hela filen import-csv .\user_group.csv | ForEach-Object {Add-ADGroupMember -Identity $_.group -Members $_.user} #Done |