If you are adding a number of users or computers within Active Directory to one or more groups it can be time consuming. I needed to add AD objects into groups, over time, sometimes with duplicate objects in the source data and the group, so created a txt file per AD group and a small script (one code block per group/file) to help with the additions.
My source text files were raw lists, plain text, one entry per line.
1 2 3 |
user1 user2 user3 |
When the script is run, it will add all the entries in the relevent .txt file into the relevent group. This allows you to add to the text file with additional lines, even if there is the odd duplicate, and providing it matches a valid AD object, it will get added!
A nice time saver…..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Write-Host -------- $grp = "GPO Rollout 0" $users1 = Get-ADGroupMember -Identity $grp $list=Get-Content c:\Users\test\Desktop\gpo0.csv ; $List | foreach {Add-ADGroupMember -Identity $grp -Members (Get-ADComputer $_)} $users2 = Get-ADGroupMember -Identity $grp $added = $users2.count - $users1.count Write-Host -foreground "green" $grp - Had: $users1.count, Now: $users2.count, Added: $added $grp = "Software - Foxit Reader PDF" $users1 = Get-ADGroupMember -Identity $grp $list=Get-Content c:\Users\test\Desktop\fox.csv ; $List | foreach {Add-ADGroupMember -Identity $grp -Members (Get-ADComputer $_)} $users2 = Get-ADGroupMember -Identity $grp $added = $users2.count - $users1.count Write-Host -foreground "green" $grp - Had: $users1.count, Now: $users2.count, Added: $added $grp = "Software - 7-Zip" $users1 = Get-ADGroupMember -Identity $grp $list=Get-Content c:\Users\test\Desktop\zip.csv ; $List | foreach {Add-ADGroupMember -Identity $grp -Members (Get-ADComputer $_)} $users2 = Get-ADGroupMember -Identity $grp $added = $users2.count - $users1.count Write-Host -foreground "green" $grp - Had: $users1.count, Now: $users2.count, Added: $added Write-Host -------- |
These groups were linked to GPS’s and software rollout tasks but they could be for anything, I have also used this for exchange mailbox operations and more.
Enjoy.
(Visited 695 times, 1 visits today)