Inserting text on the top line of a text file

This topic contains 6 replies, has 4 voices, and was last updated by  Joe Maruca 3 weeks, 2 days ago.

  • Author
  • #102899

    Joe Maruca

    I am trying to insert a text string on the very top line of a text file that has been created by the code below:

    $Output | Out-file "C:\Scripts\Status\Output\$(get-date -f yyyy-MM-dd-hh-mm-ss)_test.txt"
    Example Output File: 2018-06-21-18-25-23_test.txt

    I just want to insert one line of text in this text file; hope you can help. Thank you.

  • #102901

    Mike R.

    What type of object is $Output? Can't you add the text to it before piping to Out-File?

    • #103006

      Joe Maruca

      Hi Mike,
      Sorry, I should be been a little clearer. The $Output is simply a screen scrape from a web page; so the text file has a number of text rows. What I wanted to do is insert a line of text at the very beginning of the text file and wasn't sure whether to create the first line of text then output the web page data as an append or the other way round ... hope that makes sense, thanks.

  • #102904

    Rob Simmers

    Hey Joe. You didn't specify what is $Output, but something as simple as below adds a line of text to a file:

    $Output = "New line of text"
    $Output | Out-file "C:\Scripts\$(get-date -f yyyy-MM-dd-hh-mm-ss)_test.txt"

    If you are trying to add text to an existing file, but add it on top, then you can leverage an array:

    #An existing file with content, contains "Existing Text"
    $content = Get-Content -Path C:\Scripts\2018-06-21-08-55-27_test.txt
    #Create a new array
    $Output = @()
    #Add new text
    $Output += "New line of text"
    #Append old text from content
    $Output += $content
    $Output | Out-file "C:\Scripts\$(get-date -f yyyy-MM-dd-hh-mm-ss)_test.txt"


    New line of text
    Existing text

    If you can manipulate arrays, then you can manipulate the order of the text written to the file, even reverse the entire order:

    PS C:\WINDOWS\system32> $output= "One", "Two", "Three"
    PS C:\WINDOWS\system32> $output
    • #103007

      Joe Maruca

      Hi there Rob,
      Please see my earlier reply to Mike .... I tried your code above but didn't get the desired result. I just want to insert a line of text which is essentially a heading after the text file has been created; your thought?; thx heaps for you input here.

    • #103009

      Olaf Soyk

      Wouldn't be possible to create "another" new file with the single line you need to add to the top and then add the content of the already existing files to that new file? If needed you can delete the original output file and rename the new created one. 😉

    • #103141

      Joe Maruca

      Hello everyone,
      After playing around for a while, I managed to get the desired result; this is what I did:

      $Title = "Insert Test at Top of Text File"
      $Output = "This is TEST"
      $Title + "`r`n" + $Output | Out-file "D:\Script\$(get-date -f yyyy-MM-dd_HH-mm-ss).txt"

      Insert Test at Top of Text File
      This is a TEST

      Thanks for guidance ... take care.

You must be logged in to reply to this topic.