Avatar of IT _Admin0723
IT _Admin0723
Flag 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.
PowershellVB Script

Avatar of undefined
Last Comment
IT _Admin0723

8/22/2022 - Mon
Amit Kumar

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

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.
Amit Kumar

can you give me computer names in a text file and group name so I can give you exact script.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Amit Kumar

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
IT _Admin0723

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Amit Kumar

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 _Admin0723

ASKER
I figured out the answer.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.