locked
irritated for csv file design by powershell RRS feed

  • Question

  • I am stuck on a point where I have two array variables like this $name = @("A","B","C") $roll = @("1","2","3") and I am trying to

    make a csv file with these data. I am using these code

    $name = @("A","B","C")
    $roll = @("1","2","3")
    $report = New-Object psobject
    $report | Add-Member -MemberType NoteProperty -Name "Name" -Value "$name"
    $report | Add-Member -MemberType NoteProperty -Name "Roll" -Value "$roll"
    $report | Export-Csv -Path "C:\Users\Abby\Desktop\r.csv" -NoTypeInformation

    But I have a output like these

    NAME | ROll

    A B C | 1 2 3

    I want my output like these

    NAME | ROll

    A       |  1

    B       |  2

    C       |  3

    Can any one have any idea how to do these... Please help me... :(

     
    Wednesday, November 1, 2017 11:51 AM

Answers

  • You could do it like this:
    $name = @('A','B','C')
    $roll = @('1','2','3')
    $index = 0
    foreach($Item in $name){
        [PSCustomObject]@{
            Name = $name[$index]
            Roll = $roll[$index]
        }
        $index++
    }


    Best regards
    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, November 1, 2017 12:15 PM
    • Marked as answer by Kunal Majumder Wednesday, November 1, 2017 1:09 PM
    Wednesday, November 1, 2017 12:14 PM

All replies

  • You could do it like this:
    $name = @('A','B','C')
    $roll = @('1','2','3')
    $index = 0
    foreach($Item in $name){
        [PSCustomObject]@{
            Name = $name[$index]
            Roll = $roll[$index]
        }
        $index++
    }


    Best regards
    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Wednesday, November 1, 2017 12:15 PM
    • Marked as answer by Kunal Majumder Wednesday, November 1, 2017 1:09 PM
    Wednesday, November 1, 2017 12:14 PM
  • Thanks a lot for this solution.... :)  it works for me..
    Wednesday, November 1, 2017 1:09 PM