# \[Solved\]Adding users into groups from CSV file

**URL:** <https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382>\
**Category:** PowerShell Help\
**Created:** [March 25, 2018, 8:58am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382 "2018-03-25T08:58:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 25, 2018, 8:58am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/1 "2018-03-25T08:58:01Z")

</div>

Hi guys,  
maby i define the quastion in wrong way so, i’m sorry for that.

i have a script which is creating users from a CSV file, in that same CSV file i have a header which named “groups”  
for each user i’ve written the groups that it need to be joined, using the “Add-ADPrincipalGroupMembership” which is supposed to be used for adding one user for multiple groups.

It working great when i use it for indevidual user that’s mean for me that my syntax is correct, but i’m missing something and here is where i need your help

now here is my quastion:

1. Do i need to creat indevidual geader for each group in the CSV file? or there is a way to make the  
“Add-ADPrincipalGroupMembership” CmdLet to read all the comma seporated groups? (if i think about that in the Power Shell way, i’ll guess that i need to creat each header for each group?)

i’m adding my code and the CSV headers for better understanding:

Headers from the CSV file:  
“First Name, Last Name, SamAccountName, UserPrincipalName, DisplayName, Job Title, Department, Description, Path, ScriptPath, Groups”

Groups under the appropriate  
the groups written like that in each raw under the “Groups” header: “VPN,NY branch,HR,Terminal Server Users”

here is my code

```
cls
#get the csv file
$filepath = import-csv "C:\users.csv"

#set the variable for the uers
$newusers = $filepath

#set Passwords for new users 

$securepassword = ConvertTo-SecureString "blahblah" -AsPlainText -Force

#start the loop for adding users

foreach ($user in $newusers) {

#get user information

$firstname = $user.'First Name'.Trim()
$lastname = $user.'Last Name'.Trim()
$loginname= $user.SamAccountName
$UsrPrincipalName = $user.UserPrincipalName
$jobtitle = $user.'Job Title'
$Department= $user.Department
$Description = $user.Description
$OuPath= $user.Path
$LoginScript=$user.ScriptPath
$displayname= $user.DisplayName
$group = $user.Groups

#creat the users in active directory

New-ADUser -PassThru -Name "$firstname $lastname" -GivenName $firstname `
 `
 -Surname $lastname -UserPrincipalName $UsrPrincipalName `
 `
 -SamAccountName $loginname -Path $OuPath -ScriptPath $LoginScript `
 `
 -AccountPassword $securepassword -ChangePasswordAtLogon $false `
 `
 -Department $Department -DisplayName $displayname `
 `
 -Description $Description -Title $jobtitle -Enabled $true  

 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.Groups

 
Write-Host "`n"
Write-Host "The account for $firstname $lastname created in $OuPath successfully"

}
```

Thanks alot for your help

the CSV file:  
in this link you have a picture of the csv sample  
[CSV file](https://ibb.co/i9nz5S)

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [March 25, 2018, 9:05am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/2 "2018-03-25T09:05:03Z")

</div>

The problem is that a CSV file isn’t meant for this. A CSV is a flat file, with each row meant to represent ONE THING. But you’re trying to use it to represent MULTIPLE THINGS. You can do some hacks to make it work, but it’s always going to be more difficult because it’s not what CSV was intended for.

The “right” thing to do would be to have a SECOND CSV file, with each row listing a user name and ONE group to join them to.

---

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 25, 2018, 9:08am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/3 "2018-03-25T09:08:29Z")

</div>

Hi Don,  
what do you think,  
if i add more headers to the same CSV file like “group1” , “group 2” etc…

it will do the job?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 25, 2018, 9:09am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/4 "2018-03-25T09:09:06Z")

</div>

What’s your problem? What does not work? As you can read in the help the cmdlet [Add-ADPrincipalGroupMembership](https://docs.microsoft.com/en-us/powershell/module/activedirectory/add-adprincipalgroupmembership?view=winserver2012-ps&viewFallbackFrom=winserver2012r2-ps) takes for the -MemberOf property Distinguished Name or GUID or Security Identifier or sAMAccountName of the groups. Is it that what you have in your CSV file?

---

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 25, 2018, 9:11am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/5 "2018-03-25T09:11:35Z")

</div>

it’s ok, Don is answered my question

Thanks 🙂

and it’s working now 🙂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 25, 2018, 9:16am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/6 "2018-03-25T09:16:13Z")

</div>

Really? I had another impression.

---

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 25, 2018, 9:33am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/7 "2018-03-25T09:33:31Z")

</div>

Hi Olaf, yes it is:

here is the code:

```
cls
#get the csv file
$filepath = import-csv "C:\users.csv"

#set the variable for the uers
$newusers = $filepath

#set Passwords for new users 

$securepassword = ConvertTo-SecureString "123456" -AsPlainText -Force

#start the loop for adding users

foreach ($user in $newusers) {

#get user information

$firstname = $user.'First Name'.Trim()
$lastname = $user.'Last Name'.Trim()
$loginname= $user.SamAccountName
$UsrPrincipalName = $user.UserPrincipalName
$jobtitle = $user.'Job Title'
$Department= $user.Department
$Description = $user.Description
$OuPath= $user.Path
$LoginScript=$user.ScriptPath
$displayname= $user.DisplayName
$group1 = $user.Group1
$group2 = $user.Group2
$group3 = $user.Group3
$group4 = $user.Group4

#creat the users in active directory

New-ADUser -Name "$firstname $lastname" -GivenName $firstname `
 `
 -Surname $lastname -UserPrincipalName $UsrPrincipalName `
 `
 -SamAccountName $loginname -Path $OuPath -ScriptPath $LoginScript `
 `
 -AccountPassword $securepassword -ChangePasswordAtLogon $false `
 `
 -Department $Department -DisplayName $displayname `
 `
 -Description $Description -Title $jobtitle -Enabled $true  

 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group1
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group2
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group3
 Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.group4

 
Write-Host "`n"
Write-Host "The account for $firstname $lastname created in $OuPath successfully"

}
```

Maybe i’m asking “stupid” questions or it’s looks like i don’t read stuff or something like that, but i can assure you that i do and lot’s and lots of stuff, it’s just it takes me a bit longer than other people to understand 🙂

and with the right guidance from people like Don and other people who are helping me here, i’m understanding things 🙂

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [March 25, 2018, 1:49pm UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/8 "2018-03-25T13:49:17Z")

</div>

Glad to hear you got it working!

I’d like to point out here something that could easily cause you a few headaches in future if you keep doing it. **Backticks**.

Line-continuations in PS aren’t really hard to come by, but backticks are easily the worst method, simply because they are very easy to miss when later editing your code a few months down the line. You can spend hours tracking down the strays causing the issues – and I have, and it’s _not_ fun.

So, instead, I would highly recommend you look into **Get-Help about\_Splatting** , at least for function parameters:

```
$UserParameters = @{
    Name = "$firstname $lastname"
    GivenName = $firstname
    Surname = $lastname
    UserPrincipalName = $UsrPrincipalName
    SamAccountName = $loginname
    Path = $OuPath
    ScriptPath = $LoginScript
    AccountPassword = $securepassword
    ChangePasswordAtLogon = $false
    Department = $Department
    DisplayName = $displayname
    Description = $Description
    Title = $jobtitle
    Enabled = $true
}

New-ADUser @UserParameters
```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 25, 2018, 3:22pm UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/9 "2018-03-25T15:22:15Z")

</div>

Hmm … that’s actually not what Don recommended. 😉 And I think it’s actually not the problem with your original code. If you have a CSV file, every “cell” usually contains a **single** property. But if you put a “list” of properties into one “cell” you have to “split” them into single properties before using. So when you change your orignal command line from this

```
Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $user.Groups
```
 to this
```
Add-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf ($user.Groups -split ‘,’)
```
 it should actually work like intended.

---

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 26, 2018, 12:02am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/10 "2018-03-26T00:02:06Z")

</div>

Hi Olaf,  
Thanks alot for your information 🙂

That’s what i actually tried to achieve I wanted to use one cell with all the groups comma seporated and grab them, but i just didn’t knew how to do that 🙂

In the end as you can see, what I did is to add 4 more cells (each cell for a group) and run the “Add-ADPrincipalGroupMembership” CmdLet 4 times (as you can see) and it did the trick.

i’m on power shell abit more then a month so i don’t have so much experiance, altghout i read a lot and watch alot of youtube, MVA, CBT etc…

so each time i’m learning something new 🙂

my next goal is to make that script to send me an email with all the users that it created with the passwords 🙂

---

<div class="post-metadata">

**Author:** ![websghost](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/websghost/32/361_2.png) [@websghost](https://forums.powershell.org/u/websghost)\
**Post date:** [March 26, 2018, 2:04am UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/11 "2018-03-26T02:04:09Z")

</div>

Hi Joel,

I’m familiar with the “@” parameter, just i wasn’t so sure how to implement that so well, so i’ve tried the things that i know 🙂

Good to know 🙂

Quastion:  
I need to leave the Foreach statement as it is and just edit the code to use the @?

one more quastion:

in your code you didn’t use “'” at all near the left column :

```
$UserParameters = @{
    Name = "$firstname $lastname"
    GivenName = $firstname
}
```

As I’ve seen on youtube in one of Don’s instructions, when you use “splatting” you have to do it in that way:

```
get-wmiobject -class win32_service -computername dc01 -filter "state='running'" -namespace root\cmv2

$stuff=@{
        'class' = 'win32_service;
'computername' = 'dc01';
       'filter' = "state='running'"
    'namespace' = "root\cmv2"
       }

get-wmiobject @stuff
```

can you please explain what is the difference? or your way and my example are the same?

I can use it in both ways?

Thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/solved-adding-users-into-groups-from-csv-file/10382/12 "2024-05-16T20:36:06Z")

</div>


