Friday, July 29, 2011

Add Users to a Group–PowerShell Script

Purpose – Add users to a group from an input file – PowerShell V2 Script. 

Input file – Input file (Users.csv) contains samAccountName in the following format:

image

Script

image

 


I have also uploaded this script to Microsoft TechNet Gallery - http://gallery.technet.microsoft.com/scriptcenter/ffff189d-8ef1-4903-b19c-12dcd352c88e

More Scripts -  http://portal.sivarajan.com/search?q=scripts&x=0&y=0

24 comments:

This comment has been removed by the author.

net localgroup command is also equally simple and easy to use.
refer add user to group

Hi Santhosh,

The post is quite a useful one. I am new to this PS scripts, and would like to know, whether it reads our CSV or a text file, and add each users found in the CSV to the AD group, "TestGroup1" as in example. If it runs for a second time, the user already present in the Group, would it still add, or it skips and addthe next user? How does it work?

Thank you it helped me a lot

Hi,

what about adding users on local server rather than Domain controller ?

Needz

very helpful PS script for my application. I needed to add a long list of domain users into several admin groups. This script saved me tons of time.

richard-lee-morris,

Thanks and appreciate your feedback!

Can you please let me know how can we use the same script to add a user to a secuity group in another trusted domain, This script only works for logged in domain.

thank you for this useful easy article.

Excellent script quick question though I have AD groups which are long and are separated eg BR-Systems Support the script doesn't like the space between Systems and Support

You have a few options. Try with "GroupName" "BR-Systems Support "

Other option is to define a variable with your group name.

Import-module ActiveDirectory
$GroupName = "BR-Systems Support"
Import-CSV "C:\Scripts\Users.csv" | % {
Add-ADGroupMember -Identity $GroupName -Member $_.UserName
}

I still get an error as following ( using email adress and just the initial and last name ) still errors !

Add-ADGroupMember : Cannot find an object with identity

This comment has been removed by the author.
This comment has been removed by the author.

Try this:
Import-module ActiveDirectory
Import-CSV C:\Scripts\Users.csv | % {Add-ADGroupMember -Identity GroupName -Member $_.UserName}

without the Quotation Marks and for GroupName use your own groupname you need to use.

Chears, John

Hi, is there a way to also specify the group that each user is a member of in the CSV file and the script will add it accordingly? So there would be multiple users added to multiple groups. Can I also specify a security group under the UserName column in the CSV? Sometimes we have a security group that's a member of another group. Thanks for the help!

Hi, is there a way to also specify the group that each user is a member of in the CSV file and the script will add it accordingly? So there would be multiple users added to multiple groups. Can I also specify a security group under the UserName column in the CSV? Sometimes we have a security group that's a member of another group. Thanks for the help!

Hi Santosh,
I have to add approx 5000 users in one single AD group, i tried it your way but i couldn't do that.
i am getting this error (Add-ADGroupMember : Cannot find an object with identity).
Could you please help me into this ?.

How do i other domain users ? Like if group domain is america and you want to add a user from asia how to do it? By default it is searching the user in the group's domain

There is a typo in the script -member should be -members . Or Microsoft added an s to it in later versions

I'm getting an error when trying to run this.
Here is the script I'm using.

Import-CSV "C:\Users.csv" | % { Add-ADGroupMember -Identity Domain Users -Members $_.UserName }

The CSV file I'm using has the header UserName and the first name is Chase Chapin.

The error seems to be pointing to the CSV file. I'm putting the first error here, since they all seem to be the same.

Add-ADGroupMember : A positional parameter cannot be found that accepts argument 'Users'.
At line:1 char:33
+ Import-CSV "C:\Users.csv" | % { Add-ADGroupMember -Identity Domain Users -Member ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-ADGroupMember], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

Thank you,
Dave


Param(
[string] $group # List of servers that needs to be checked. Has to a text file!
)

$UsersCSV = Import-Csv "C:\script\adgroup\adgroup.csv" | select -ExpandProperty identity
#$list = Get-Content $UsersCSV

foreach ($user in $UsersCSV)
{
$u = (Get-ADUser -Identity $user).distinguishedName
$g = Get-ADGroupMember -Identity $group | select -ExpandProperty distinguishedname

If ($g -contains $u ) {
Write-Host " $user already exists in this group" >> result.txt
}
Else {
Add-ADGroupMember $group -Members $u
Write-host " $user added to group successfully” >> result.txt
} }

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More