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

IT_Admin XXXX
IT_Admin XXXX used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
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.

Comp1$
Comp2$
Comp3$

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

Author

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.
Top Expert 2015

Commented:
can you give me computer names in a text file and group name so I can give you exact script.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Top Expert 2015

Commented:
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

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 {"$_$"}
Top Expert 2015

Commented:
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

Author

Commented:
I figured out the answer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial