Simple powershell text to CSV convert issue

Welcome Forums General PowerShell Q&A Simple powershell text to CSV convert issue

This topic contains 5 replies, has 2 voices, and was last updated by

 
Keymaster
8 months, 2 weeks ago.

  • Author
    Posts
  • #94909

    Participant
    Points: 0
    Rank: Member

    I have apparently misunderstood something about converting txt to csv through powershell

    My txt file looks like this: The original looks the same but is much larger.

    OS Users
    Win98 Admin1
    Win7 Admin2
    Win8 Admin3
    Win8.1 Admin4
    Win10 Admin5

    I would like some help to conert the text file to csv with comma as a delimeter.
    Here is my code:

    Get-Content C:\PS-import\TXT-TO-CSV.txt | ConvertTo-Csv | Export-Csv C:\PS-import\News.csv -NoTypeInformation
  • #94911

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    So, the only delimiter in the current file is a space between columns?

  • #94912

    Participant
    Points: 0
    Rank: Member

    Hi Mr. Jones
    Yes it is.

  • #94914

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    Then try:

    Import-Csv filename.txt -delim " " | Export-CSV filename.csv

    ConvertTo-CSV only works with structured objects, not text. Import-Csv can create structured objects, and you can give it delimiters other than a comma.

  • #94915

    Participant
    Points: 0
    Rank: Member

    Hi Mr. Jones
    Now the csv file looks like this, also it uses tabulator as delimeter, I forgot.

    "OSUsers"
    "Win98Admin1"
    "Win7Admin2"
    "Win8Admin3"
    "Win8.1Admin4"
    "Win10Admin5"

  • #94932

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    Well, if it's tab delimited, then you need to specify that as a delimiter. "`t" is a tab character.

The topic ‘Simple powershell text to CSV convert issue’ is closed to new replies.