How add members into a group using a csv file
5 Spice ups
craigduff
(cduff)
2
You’re leaving a lot of details up to my assumptions but here goes:
Get-ADGroup -Identity GroupName | Add-ADGroupMember -Members (Import-CSV .\test.csv | Select-Object -ExpandProperty name)
3 Spice ups
chamele0n
(Chamele0n)
3
Indeed a lot of assumptions. We definitely need more information on what you are trying to accomplish before we can give a definitive answer.
Check this for some more advanced options when creating groups and members:
Wild guess you are trying to add users in active directory. I hope this helps out
Open Notepad and type the following lines. Each bullet is one line of text in Notepad but
do not include the bullets:
-
Save the file to your Documents folder with the name “Importgroups.csv” including the
quotes so that Notepad doesn’t add a .txt extension.
-
Open a command prompt and type the following command:
csvde -i -f “%userprofile%\importgroups.csv”
-
Open the Active Directory Users And Computers snap-in and check to confirm that the
groups were created successfully. You might need to refresh the view if the Active Direc-
tory Users And Computers snap-in was open prior to performing the step.
Import-module ActiveDirectory
Import-CSV “C:\Scripts\Users.csv” | % {
Add-ADGroupMember -Identity TestGroup1 -Member $_.UserName
}
… or you can use a vendor tool that does all this for you. LOL
1 Spice up
martin9700
(Martin9700)
7
Well, technically you just DID use a vendor tool to do it for you 
4 Spice ups
Good point - since it is PowerShell, should should have added the standard "Use at your own risk" statement?
1 Spice up
martin9700
(Martin9700)
9
We’re IT. Everything we do is “use at your own risk”. It goes with the job.
Is there a specific step I should be taking when running this in powershell because I receive an error stating this. Check the attached document
craigduff
(cduff)
11
Does your csv file have a UserName field?
craigduff
(cduff)
13
Do you have any lines where that field is blank, or any blank lines at the end of the file (open it in notepad or something)?
This works fine but I realize that we are using display names. Would this work with those properties?
craigduff
(cduff)
15
Nope, this is from the help file on what can be passed to that parameter:
Distinguished Name
Example: CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com
GUID (objectGUID)
Example: 599c3d2e-f72d-4d20-8a88-030d99495f20
Security Identifier (objectSid)
Example: S-1-5-21-3165297888-301567370-576410423-1103
SAM Account Name (sAMAccountName)
Example: saradavis
1 Spice up
artb
(ArtB)
16
But, it’s pretty easy to build a conversion hash table…
$hash = @{}
Get-ADUser -filter * | foreach {$hash.Add($_.Name,$_.SamAccountName)}
Then you can use $hash[$_.username] where ever you need the SamAccountName.