Query - import-csv

Hi, just a quick one.

This does work, just wondered if there was a better way to achieve this

While creating distribution groups in o365, I’m taking the Owners column that has a list of comma separated ‘firstname surname’ and I want to convert that to our corporate email address in the correct format to add the managedBy users

Import-Csv .\csv.csv | foreach {
    $owners = ""$($_.Owners.Replace(',','@emaildomain.com","').Replace(' ','.')+"@emaildomain.com")""


The first replace() looks like it’s redundant as you’re not specifying a delimiter for Import-CSV. If you have commas in your column you’re only going to get the first item in the comma separated list plus the domain.


Thanks for the reply, sorry meant turning

user1 surname1,user2 surname2,user3 surname3



So its multivalued

This worked, but is it the best option?

$owners = $($.Owners.Replace(’,’,’@emaildomain.com,’).Replace(’ ‘,’.’)+"@emaildomain.com")
$owners = $owners -split ’ *, *’
$members = $($
.Members.Replace(’,’,’@emaildomain.com,’).Replace(’ ‘,’.’)+"@emaildomain.com")
$members = $members -split ’ *, *’


I don’t think there’s anything wrong with it :slight_smile:

You could keep it all on one line and pipe the split objects to another ForEach-Object which I think helps readability.

Import-Csv E:\temp\owners.csv | 
    foreach {$_.owners -split ',' | 
        foreach {$_.Replace(' ','.')+'@emaildomain.com'}}