Link to home
Start Free TrialLog in
Avatar of IT _Admin0723
IT _Admin0723Flag for United States of America

asked on

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.
Avatar of Amit Kumar
Amit Kumar
Flag of India image

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

Avatar of IT _Admin0723

ASKER

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.
can you give me computer names in a text file and group name so I can give you exact script.
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

ASKER CERTIFIED SOLUTION
Avatar of IT _Admin0723
IT _Admin0723
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
I figured out the answer.