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

58 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?

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
} }

Where can I go to find out more about powershell. I've never used it before and now it's become increasingly important that we all know how to use it...

Thanks for sharing it is important for me. I also searched for that from here. Visit our site panda helpdesk telefoon

That’s a nice article, thank you for a great article. It helped me a lot. Keep it up Must Visit panda contact belgie

Really Nice Post Admin, Very helpful looking for more posts, Now I have to share some information about How To Fix “Bullguard Troubleshooting Guide” problem. If you are going through this problem you can simply visit Servicio al cliente Bullguard

Don’t think you can’t do this because you can do this easily when you are going to make your boring life into the most romantic and hot life with the company of Dharamshala Escorts.
Dehradun Escorts
Dehradun Escorts

Take Indian High Profile Trusted Delhi Escorts Services
We are famous among personalities in Delhi city direct pay the hand payment Delhi Escorts Services and Direct Deal With Our Girl at your place or my Escort In Delhi.
Delhi Escort
Delhi Escorts

We are the best and profoundly clandestine office that has been giving agency In Haridwar Escorts for quite a while. We are known not to frustrate anybody so visit our site.

Haridwar Escorts

You can have a wide range of fun delights with great Haridwar Escorts from all around the globe So on the off chance that you also have any unfulfilled wants need wedding night experience, single man gathering, or birthday celebration festivities, simply call our site and discover the delights of paradise directly close to you.

Haridwar Escorts

Haridwar Escorts Service

Hey, Guys, I'm Neha Sharma, a model call girl in Cuttack Escorts. If you are looking for Russian call girls so visit our site and contact us because we are a night service provider and we are here for you.

Cuttack Escorts

Hey Guys I'm Neha Sharma and sexy call girl in Haridwar Escorts. if you are looking for model escorts so visit our site and contact us because we are the sexual activity service provider and we are here for you.

Haridwar Escorts

Check in the luxury hotel to explore the joy of nailing a sexy girl escort in Cuttack. Hire our Cuttack Escorts service to enjoy your nights on Odisha Trip.

Cuttack Escorts

Coimbatore is not the only destination we reach instead you can appoint our best call girls service in Chennai as well. We are Coimbatore Escorts agency since 2010.

Coimbatore Escorts

Enjoy these amazing sex positions With Haridwar Escorts to fulfill your desire practically all you love to have intercourse and need to feel the excellence of this astonishing robust involvement.

Haridwar Escorts

Relationships are the most amazing things for men and when you are thinking to ensure the bold relationship possibilities for romantic hookups then you must connect with the services of Pathankot Escorts.

Pathankot Escorts

Pathankot Escorts

Pathankot Escorts

A Seductive night is mainly related to hot things. On a seductive night, you can enjoy sexy things with your partner, and Haridwar Call Girls is the right choice for the clients to ensure these things.

Haridwar Escorts

Haridwar Escorts Service

Russian Escorts mean you are going to book the premium material for sex in your life. It would help if you availed yourself of the premium things for the physical relationship goals so contact us.

Rajouri Garden Escorts

Rajouri Garden Escorts Service

Without a doubt, you may have your time any way you want with the top dates and stunning Pathankot Escorts available online. You can enjoy this fantastic experience wherever you choose.

Pathankot Escorts

Pathankot Escorts Service

We know that various individuals visit Independent Delhi Escorts, for potential employee meetings, administration, etc. You can contact our maintenance administration at any time; Pooja for your service. We are free 24 hours a day. That way, it's possible to reach out to us when you feel you need more joy and satisfaction in your daily life. As the night progresses, our maidens will keep you running and lighting up the whole day. We have Delhi Escorts, you can't find anywhere else.

Thank you so much for sharing the informative post, I appreciate your work. Go so many useful and informative links. Loved your writings also.
Best Makeup Artist Dwarka
Best Makeup Artist

In whichever place you wish to have the presence of the babes here at our Pune Escorts agency, they can be adjusted to any place.
VIP Independent Pune Escorts
call Girls in Pune
Pune Escort Service
Pune Escorts Agency

It would be some of the incredible moments of love and erotic fun with these passionate skilled partners over here.
CNN-News18 escorts in Chandigarh
Chandigarh escort service Arise News

The Escorts in City can remain engaged to anyone owing to their skills and commitments all around.
Chandigarh escort Brigada News TV
escort service in Chandigarh ARY News

Amritsar Escort service have all sorts of skills in meeting the desires of men whenever they require. It is fine characteristics of these ravishing hot babes which is going to call out clients from all parts of this world.
Follow us:-

Hi Profile Call Girls in Amritsar
Amritsar Escorts Service
Amritsar Escorts
Amritsar Call Girls
Russian Escorts Service in Amritsar

Appoint Call Girls in Jammu to different programs .Whether the kind of occasion is an official meeting, corporate event or some other special program, the divas here do have patience to handle all customers.Follow us:-
Follow us:-

Jammu Escorts Service
Jammu Escorts
Jammu Escorts Service
Jammu Escorts
Jammu Escorts Service
Jammu Escorts
Jammu Escorts Service

Zirakpur call girls are suppose to perform well being linked with their clients owing to their wide range of experience and knowledge.
Follow Us:-

Zirakpur Escorts
Zirakpur Escorts Service
Zirakpur Escorts
Zirakpur Escorts
Zirakpur Escorts Agency
Zirakpur Escorts
Zirakpur Escorts

Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website.
토토사이트
온라인경마

Hi there colleagues, its fantastic piece of writing concerning tutoringand fully defined, keep it up all the time.
온라인카지노
토토사이트

It loops through each user in the $users table and tries to add them to the specified group. Errors detected during the process are recorded and displayed on the console. Now I am stuck with my academic assignment. Dissertation methodology writing help. If someone knows my topic better, then suggest me professionally. Thanks for sharing a great experience.

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More