Import multi-value csv cells into Powershell

I have a csv that goes something like this:

DLName,member

DistList1,fergus,john,ian

DistList2,fergus,James,Angus

I need to create DistList1, DistList2 etc and add as members all those users after it. There are hundreds of users, and 2800 groups.

I can create the groups ok by using:

$file = import-csv c:\DLs.csv

foreach ($line in $file) {New-DistributionGroup -name $line.DLName}

but no idea how to add the users.

I've changed the data so it reads:

DLName,member

DistList1,"fergus,john,ian"

i.e. all members/users are in the same cell, but when I try:

Import-Csv c:\dls.csv | foreach {add-adgroupmember $_.DLName -Members $_.members}

it gets upset because 'fergus,john,ian' is not a single user. I need to split them somehow.

Can someone help?

Thanks in advance,

F.

July 2nd, 2015 4:21pm

Hi,

Here's an example you can play with:

$str = 'fegus,john,ian'
$strSplit = $str -split ','

$strSplit

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 4:29pm

Genius, thanks. I knew it would be simple! ;-)

I used the following:

$file = import-csv c:\DLs.csv

foreach ($line in $file) {$split = $line.members -split ',' ; New-DistributionGroup -name $line.DLName -members $split}


July 2nd, 2015 4:58pm

Cheers, you're very welcome. Glad it worked out for you.
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2015 6:10pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics