Link to home
Start Free TrialLog in
Avatar of Justin Tucker
Justin Tucker

asked on

Powershell issue with Bulk adding users from csv.

Good Day,


Looking to get a bulk csv list of SamAccountName's added to a AD group was wondering if any one had any suggestions.  


Currently using this script:

Import-Module ActiveDirectory
$Users = Get-Content "C:\Users\******\Desktop\*******" | ForEach-Object {Add-ADGroupMember -Identity "GroupName" -Members $_.'User-Name'}

Open in new window


Getting error message "Add-ADGroupMember: Cannot validate argument on parameter 'Members'. The argument is Null or empty.


Can anyone assist with this?

Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Hello, Justin Thank you for your question!

If you're using a CSV, then you don't need to use Get-Content you use Import-CSV instead, and you're trying to save that execution into a var that doesn't provide anything :)

this would be my correction:

single liner:
Import-csv "C:\Users\Desktop\FILE.CSV" | %{
   Add-ADGroupMember -Identity "GroupName" -Members $_.'User-Name'
}

Open in new window

 CSV  for this one must contain the field: "user-name" like this:
User-Name,Name,LastName
jortega@test.com,Jose,Ortega

Open in new window

Multiple Lines
$users = Import-csv "C:\Users\Desktop\FILE.CSV"
$groupName= "This is the name of the Group"
foreach($user in $users){
   Add-ADGroupMember -Identity $groupName -Members $_.'User-Name'
}

Open in new window


This multiline version also uses the same CSV. no other changes are required.


Multiline in case the users  already exist but you do want to continue :)

Multiple Lines
$users = Import-csv "C:\Users\Desktop\FILE.CSV"
$groupName= "This is the name of the Group"
foreach($user in $users){
   try{
      Add-ADGroupMember -Identity $groupName -Members $_.'User-Name' -ea stop
   }
   catch{
      write-host -foregroundcolor yellow "There was an error trying to add $($_.'User-Name') into the group $GroupName with the error $($_.Exception.Message)"
   }
}

Open in new window


Avatar of Justin Tucker
Justin Tucker

ASKER

Hey Jose,

Thanks for your help, it didn't work I adjusted the csv file to state 'User-Name' like you suggested however, I'm still running into the Cannot Validate argument on parameter 'Members' message.  Any thoughts?
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of 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
HI Jose!

Thanks that worked perfectly! Thanks for your help!
I'm glad to help Justin!