Add AD Users to different groups from a csv file with powershell

HI EE

I have a csv file with a list of SamAccountnames and group names .. anyone have a script to share that will add each dedicated group to each account ?  I can change the csv headed to what ever  ..

Capture1.PNG
LVL 2
MilesLoganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
Use the following script below to add these members to the groups.

Change the Heading for Group_Name to Group.
Import-module activedirectory
$Users = Import-Csv "c:\filename.csv"
ForEach ($User in $Users)
    {
        Add-ADGroupMember -Identity $User.Group -User $User.SamAccountname -Confirm:$false

}

Open in new window


Thats it.

Will.
0
MilesLoganAuthor Commented:
Hi Will ..

I changed he headed to "group" and I tested it with User and SamAccountname and I receive the error below.

Add-ADGroupMember : A parameter cannot be found that matches parameter name 'User'.
At C:\PS\AddGroupstoUsers.ps1:5 char:42
+     Add-ADGroupMember -Identity $User.Group -User $User.SamAccountname -Confirm:$fa ...
+                                             ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-ADGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
 
Add-ADGroupMember : A parameter cannot be found that matches parameter name 'User'.
At C:\PS\AddGroupstoUsers.ps1:5 char:42
+     Add-ADGroupMember -Identity $User.Group -User $User.SamAccountname -Confirm:$fa ...
+                                             ~~~~~
0
Rich BullockIT Systems EngineerCommented:
I think you need to change the heading of the group column to "DN" for distinguished name and then add the DN of each group where you have group names.
To find the DN of each group you can go to the attributes tab of the group from active directory users and computers. You will need to have "advanced features" ticked to see the tab.
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

FOXActive Directory/Exchange EngineerCommented:
Miles,
In your .csv file change the samaccountname heading to UserName, your other heading is acceptable for the group.  I have attached the powershell script you want to run.   Make sure you change the extension from .txt to .ps1 as Experts Exchange doesn't accept .ps1 attachements.  Wherever you have saved the .ps1 file I want you to cd to that location and then in powershell
your syntax will be  .\Addtogroups.ps1  **then press enter and let it go!! :) **

**right-click powershell and run as administrator**
Addtogroups.txt
0
MilesLoganAuthor Commented:
Hi FoxLuv .. I receive a similar error with yours ..

PS C:\PS> C:\PS\Add2GroupsTEst.ps1
Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null
argument and try the command again.
At C:\PS\Add2GroupsTEst.ps1:3 char:29
+ Add-ADgroupmember -Identity $_.group_name -Member $_.UserName
+                             ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGr
   oupMember
 
Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null
0
FOXActive Directory/Exchange EngineerCommented:
At the top of your .csv file, for the groups is it still reading group_name or did you change it?
0
MilesLoganAuthor Commented:
I changed it Group .. i tested both options below.

SamAccountName,Group and UserName,Group
0
FOXActive Directory/Exchange EngineerCommented:
Tell me what the top of your .csv file reads now or send me a screen shot of your .csv .  My script has to match your headings or it will not work.
0
MilesLoganAuthor Commented:
Column A = SamAccountName
Column B = Group
0
FOXActive Directory/Exchange EngineerCommented:
Import-module activedirectory
Import-csv "pathtoyourcsvfile.csv" | % {
Add-ADgroupmember -Identity $_.Group -Member $_.SamAccountName
}


Put this into a .ps1 and try it again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
That did it .. thanks !
0
FOXActive Directory/Exchange EngineerCommented:
Your welcome buddy!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

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.