Powershell to Add Bulk Computers (Host Name Format) to a Global Group in Active Directory

Hello experts,

I need to add 5500 computer objects to a global group in Active Directory. Does anybody have a handy script (VB or powershell) that will read the list of computers from txt or csv and send an output result which ones were successful and not because it was already in the group.

Please help!

Thank you.
IT_Admin XXXXAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amit KumarCommented:
prepare a csv file with computer Name and run below powershell:

Computer name should be mentioned like as below mentioned and powershell should be run in AD powershell module.


Change $groupname variable in script with your group name

import-module ActiveDirectory
$groupname = "please specify group name here"
$comps = Import-csv c:\comp.csv
foreach ($comp in $comps) {ADD-ADGroupMember $groupname –members $comp
write-host "$comp is added to the $groupname."}

Open in new window

IT_Admin XXXXAuthor Commented:
Thank you for the fast response! Unfortunately, it did not work :(... And also need an output detailing if it was added or not to the group.
Amit KumarCommented:
can you give me computer names in a text file and group name so I can give you exact script.
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Amit KumarCommented:
try this one, and please copy this code in a text file then save it it to .ps1 file then run it.

for report what you can do just export members of that group and compare it with your csv file that way will be good else it will take a lot time to verify it by script, because all the time we will have check if that computer is added or not and by default it does not mark a flag which can be compared with if/else condition.

Please paste error while running this code.

import-module ActiveDirectory
$comps = Import-csv c:\comp.csv
foreach ($comp in $comps) 
$groupname = "please specify group name here"
ADD-ADGroupMember -id $groupname –members $comp
write-host "$comp is added to the $groupname."}

Open in new window

IT_Admin XXXXAuthor Commented:
I tested this and this one worked...I just need to write an output which ones were successful and which ones were not successful. Please help! Thanks!

Get-Content c:\workstations.txt | Add-ADPrincipalGroupMember -memberof 'WSUS Auto Download and Notify for Install' -identity {"$_$"}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amit KumarCommented:
this command can't give you output in data file, for this export this group data in a csv file and compare it with original

Get-ADGroupMember "Groupname" | select * | export-csv c:\comp.csv
IT_Admin XXXXAuthor Commented:
I figured out the answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.