CSV - row RRS feed

  • Question

  • Hey!

    I have csv:

    Import csv:

    group, members

    group1, member1 member2 member3

    group2, member4 member2 member3 member5

    And now i want to transform csv to this:

    Export csv:

    group1, member1

    group1, member2

    group2, member4


    Can you help me?

    I have only this:

    Import-Csv "C:\csv\test1234.csv" -Delimiter "," | 
    ForEach-Object {$_.Members -replace " ",","}  
    ForEach-Object | Set-Content "C:\csv\przecinki.csv" 

    • Edited by Kakero3 Monday, January 22, 2018 8:10 AM
    • Moved by Bill_Stewart Monday, March 12, 2018 8:34 PM Unanswerable drive-by question
    Monday, January 22, 2018 7:45 AM

All replies

  • I don't understand why you need something like this but you need a nested foreach loop iterating through each member of each group. 

    In your code: what is $_.numery? To split your members into single items you could use -split "\s+" to seperate them on the space charachters.

    Best regards,


    Monday, January 22, 2018 8:04 AM
  • I need this csv form to imports memebrs to groups in Office 365.

    $_.numery are a members in second column. 

    Monday, January 22, 2018 8:09 AM
  • The file is not a CSV.  It is a text file wirh variable length lines of comma separated items.  It cannot be loaded as a CSV,

    Load it as a text file and split the lines.  Generate custom objects as needed and export to a csv.


    Monday, January 22, 2018 8:11 AM
  • So what i need to do now?
    Monday, January 22, 2018 8:53 AM
  • Start by learning PowerShell.  Learn PowerShell  

    help Get-Content -online

    You need to learn how to read files and manipulate strings as well as create custom objects.


    Monday, January 22, 2018 8:57 AM
  • I have blank output... Why?

    $csv = Import-Csv "C:\csv\test1234.csv" -Delimiter "," |
    ForEach-Object {$_.Members -replace " ","," } 
    Foreach($row in $csv){
    Write-Host $row.group
    Write-Host ($row.members)

    I use [-replace " ",","] cause my members in csv look like mem1 mem2 mem3 and i want mem1,mem2,mem3 

    • Merged by Bill_Stewart Monday, January 22, 2018 3:10 PM Duplicate
    Monday, January 22, 2018 12:00 PM
  • Hi Kakero3,

    I'm guessing that you're trying to have the group and then the members be outputted to the screen?

    This is the test CSV I created to test this:

    This is the code I used:

    $csv = Import-Csv "C:\PATH.csv"
    foreach ($i in $csv){
      $groupname = $i.group
      $member = $i.members -replace " ",","
      Write-Host $groupname - $member

    and this is the output: 

    You can simply add more groups to each row in the CSV and each member will be separated by the comma. I would recommend researching PowerShell and the foreach function as I think you're getting a bit confused.

    Hope this helps :)

    Monday, January 22, 2018 12:15 PM
  • DUPLICATE: https://social.technet.microsoft.com/Forums/scriptcenter/en-US/a2758e72-1494-4ec2-8a12-1d2923aa1270/csv-row?forum=ITCG#8d036fb5-d5db-4922-a1ce-ccd0d17bc9f8

    I told you in the original thread that you file is NOT a csv file.  You must open it as a text file and split the lines on the comma then out put as objects.

    This is why you must take the tine to learn PowerShell.

    A moderator will combine your threads so people do not try to make guesses based on incorrect information.  Without basic understanding of PowerShell and things like what  CSV is you will not be able to ask a correct question or understand the answer.'


    Monday, January 22, 2018 12:50 PM