locked
Move Text content to Excel RRS feed

  • Question

  • HI team,

    I have a number of text file in a folder. each file gives information like hostname, OS, username, service pack etc. about individual machine. Example of one of the file

    --------------------------------------------------------------------------------------

    Hostname = desktop12456

    OS = Windows 7

    username = Joe B

    --------------------------------------------------------------------------------------

    I want to collect this information of all text files in excel sheet as

    -------------------------------------------------------------

        Hostname          OS          Username

    desktop12346       Win 7          Joe B

    laptop123456        win 8          mick s

    .

    ---------------------------------------------------------------

    Could please help give a idea which cmdlets can I use in writing a script ?

    Saturday, June 14, 2014 7:14 PM

Answers

  • Where did the original text files come from?

    I would correctly design the text files in the first place (e.g., using CSV files) rather than writing a custom parser that works on the data afterwards.

    This is because using built-in data formats is always going to easier and less error-prone than using custom data formats.

    This is particularly true if you're not a pretty experienced script writer.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Wednesday, July 2, 2014 3:03 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 9:57 PM
    Saturday, June 14, 2014 7:20 PM

All replies

  • Where did the original text files come from?

    I would correctly design the text files in the first place (e.g., using CSV files) rather than writing a custom parser that works on the data afterwards.

    This is because using built-in data formats is always going to easier and less error-prone than using custom data formats.

    This is particularly true if you're not a pretty experienced script writer.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Wednesday, July 2, 2014 3:03 PM
    • Marked as answer by Bill_Stewart Tuesday, July 15, 2014 9:57 PM
    Saturday, June 14, 2014 7:20 PM
  • To do this we can use the converter.

    dir \temp\*.txt | 
         ForEach-Object{
            $data= Get-Content $_|Out-String | ConvertFrom-StringData
            New-Object PsObject -Property $data
        }
    

    Each file will be converted into a new object.


    ¯\_(ツ)_/¯

    Saturday, June 14, 2014 8:57 PM
  • This text script is generated from the scripts which runs on each computer.

    I will try to modify it to csv but could you help me in above case.

    Sunday, June 15, 2014 11:44 AM
  • This text script is generated from the scripts which runs on each computer.

    I will try to modify it to csv but could you help me in above case.

    The script I posted does what you ask.


    ¯\_(ツ)_/¯

    Sunday, June 15, 2014 12:02 PM