This topic contains 6 replies, has 4 voices, and was last updated by
June 21, 2018 at 9:31 am #102899
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.
June 21, 2018 at 12:51 pm #102901ParticipantTopics: 4Replies: 30Points: 6Rank: Member
What type of object is $Output? Can't you add the text to it before piping to Out-File?
June 22, 2018 at 9:37 am #103006
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.
June 21, 2018 at 1:14 pm #102904ParticipantTopics: 8Replies: 1190Points: 639Rank: Major Contributor
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" [array]::Reverse($output) PS C:\WINDOWS\system32> $output Three Two One
June 22, 2018 at 9:42 am #103007
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.
June 22, 2018 at 10:24 am #103009ParticipantTopics: 1Replies: 1325Points: 1,651Rank: Community Hero
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. 😉
June 24, 2018 at 9:47 am #103141
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.
The topic ‘Inserting text on the top line of a text file’ is closed to new replies.