Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

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

User generated image
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

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.
Avatar of MilesLogan

ASKER

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 ...
+                                             ~~~~~
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.
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
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
At the top of your .csv file, for the groups is it still reading group_name or did you change it?
I changed it Group .. i tested both options below.

SamAccountName,Group and UserName,Group
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.
Column A = SamAccountName
Column B = Group
ASKER CERTIFIED SOLUTION
Avatar of FOX
FOX
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it .. thanks !
Your welcome buddy!!