convert text to csv

Welcome Forums General PowerShell Q&A convert text to csv

Viewing 5 reply threads
  • Author
    Posts
    • #282592
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      Hi

      Am trying to convert text file, that containing this.

      dsafdsafds1
      fdasfasdfa2
      wqewqreerw3
      xcvzxcvxzc4
      uyiuoyiuoy5
      hjklhjlhkl6
      nmnmmmmnmm7

      My Powershell file can read the text file and export to CSV but I am doing something wrong.

      The result is like this in my CSV file

      Number  UserID
      1
      dsafdsafds1
      2
      fdasfasdfa2
      3
      wqewqreerw3
      4
      xcvzxcvxzc4
      5
      uyiuoyiuoy5
      6
      hjklhjlhkl6
      7
      nmnmmmmnmm7

      As you can see, “UserID” values are ending up under Number, and not to the right of Number.

      • This topic was modified 3 weeks, 2 days ago by 08Gamer75.
    • #282601
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero

      pls provide sample text files, sample desired output, and explain what you want to do – convert what to what?

    • #282607
      Participant
      Topics: 9
      Replies: 706
      Points: 2,837
      Helping Hand
      Rank: Community Hero

      I’d say make it simple and easy on yourself by putting powershell to work for you.

    • #282685
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,987
      Helping Hand
      Rank: Community Hero

      This can be accomplished with a simple for loop as well:

      Output:

    • #282733
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      pls provide sample text files, sample desired output, and explain what you want to do – convert what to what?

      Hi

      The text file (simple notepad .txt) named userlist.txt Contains
      dsafdsafds1
      fdasfasdfa2
      wqewqreerw3
      xcvzxcvxzc4
      uyiuoyiuoy5
      hjklhjlhkl6
      nmnmmmmnmm7

       

      The result I want to achieve is

      Number Userid
      1 dsafdsafds1
      2 fdasfasdfa2
      3 wqewqreerw3
      4 xcvzxcvxzc4
      5 uyiuoyiuoy5
      6 hjklhjlhkl6
      7 nmnmmmmnmm7

       

      The goal with this is

      The text file is a symbolic file with pretend user users.
      That I pull information about from AD (I do not have access to Active Directory so I will add that part later to the script).
      The result will be exported to a CSV file.

      But I can’t get the userid and number on the same row.
      As a start thing, until I get back to work, when I do that I will start adding “fetch information from Active Directory”.

       

    • #282739
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      $stream_reader = New-Object System.IO.StreamReader -ArgumentList C:\temp\userlist.txt   $numberOfValues = 0   $employees = while ($current_line = $stream_reader.ReadLine()) {     Write-Host $current_line       [PSCustomObject]@{         UserName = $current_line         ID       = ++$numberOfValues     } }   $employees | Export-Csv -Path C:\Temp\Employees.csv -NoTypeInformation   Write-Host $numberOfValues user was found in the file!

      Thank you,
      That was exactly what I want to achieve, and the code looks so much better, thanks to you.
      🙂

      I changed places on
      ID = ++$numberOfValues
      UserName = $current_line

      Merry Christmas  and Happy New Year

Viewing 5 reply threads
  • You must be logged in to reply to this topic.