Solved

Add multiple users to AD group with Powershell

Posted on 2014-10-01
8
573 Views
Last Modified: 2014-10-04
Hi EE

Can someone school me on a better way of adding multiple users to an AD group ? the script below seems to take along time.
Wondering what others are using ..



Foreach ($User in GC C:\PS\Users.txt){
GC C:\PS\groups.txt | % {
Add-ADGroupmember -identity $_ -member $User
	}
}

Open in new window

0
Comment
Question by:MilesLogan
  • 3
  • 3
  • 2
8 Comments
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 40356090
$users = GC C:\PS\Users.txt
GC C:\PS\groups.txt | % {
$gname = $_
$users | % {Add-ADGroupmember -identity $gname -member $_}
      }
0
 
LVL 39

Expert Comment

by:footech
ID: 40356119
You can specify an array for the -members parameter of Add-ADGroupMember.
$Users = GC C:\PS\Users.txt
GC C:\PS\groups.txt | % {
Add-ADGroupmember -identity $_ -members $Users
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 40356133
This will work ! thank you
0
 
LVL 39

Expert Comment

by:footech
ID: 40356165
I haven't tested, but I would be highly surprised if becraig's performs any different than what you originally posted.
0
 
LVL 28

Expert Comment

by:becraig
ID: 40356172
So Footech you think multiple reads from disk will have the same overhead as looping through an array ?
0
 
LVL 39

Expert Comment

by:footech
ID: 40357042
No, you're absolutely right. The single read will definitely be faster.  For some reason I was only focused on the multiple calls to Add-ADGroupMember.  But now you've got me curious about the relative speed of the disk read vs. the AD call.
0
 
LVL 28

Expert Comment

by:becraig
ID: 40357092
Incidentally I think your approach is faster single ad call against the array.

I just provided one solution slightly faster than the author had initially been using.
0
 
LVL 2

Author Comment

by:MilesLogan
ID: 40361118
Thanks guys , I appreciate the additional comments .. You both are awesome 🙆!
0

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now