Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

Adding users into group automatically - Windows 2008 R2

I have csv file containing list of sAMAccountNames of the users, I want to add all users into group automatically without manual intervention.

How can this be done. Do i need to use script?

thanks
0
tech2010
Asked:
tech2010
  • 5
  • 4
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
here's a powershell script to add users to group.
change the csv file and the group name.
this script code assume no header in the csv.
Import-CSV p:\test.Csv | %{add-Adgroupmember -Identity 'AD_Group' -members $_}

Open in new window

0
 
tech2010Author Commented:
How to create the script, is it with .ps1

Also how it will run as schedule task? I have UAC enabled so how can i run this as administrator?
0
 
tech2010Author Commented:
Also if a user already member of the group, can we put someone in the script e.g if user is member of the group then don't attempt to add into the group otherwise it will error. I would also like to email output result of added users into group.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Meir RivkinFull stack Software EngineerCommented:
save this as xxx.ps1:
Import-CSV p:\test.Csv | %{
$user = $_
if(@($members | Where-Object{$_.Name -eq "Ariel Fridman"}).Count -eq 0){
add-Adgroupmember -Identity 'AD_Group' -members $user
}
}

Open in new window

how often would u like to run this as task scheduler?
0
 
tech2010Author Commented:
do i need to run this on domain controller or can i run this even on the server where RSAT tools are installed?

if(@($members | Where-Object{$_.Name -eq "Ariel Fridman"}).Count -eq 0){

What is Ariel Fridman? can you explain what this statement means?
0
 
Meir RivkinFull stack Software EngineerCommented:
Sorry my mistake:
Import-CSV p:\test.Csv | %{
$user = $_
if(@($members | Where-Object{$_.Name -eq $user}).Count -eq 0){
add-Adgroupmember -Identity 'AD_Group' -members $user
}
}
0
 
Meir RivkinFull stack Software EngineerCommented:
Save it as xxx.ps1 and run it from command line:
Powershell xxx.ps1

Make sure to update the csv file path in the script and the group name
0
 
tech2010Author Commented:
I want to automate this and run this as schedule task daily.

Also, I want to email the results/output of this script that what are the users added into the group? How to do this? thanks

So tell me what your script will do, will it check if user exist in the group then it will skip and end the script but if user not already in the list then i will add? thanks
0
 
tech2010Author Commented:
Also, in my csv file has header 'SAM Account Name' so i guess variable in the script will be like this $user = $_"SAM Account Name"

please verify and update the script.

please also read my last post

many thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now