Home > Software engineering >  How to add content at the start of page powershell?
How to add content at the start of page powershell?

Time:10-28

I have a sample script, which reads a file and add a text in front of text loaded and write back as new file with combined data.

Problem is the original lines are replaced with spaces.

$r = Get-Content -Path "C:\\Users\\UReddy\\Desktop\\travelling - Copy (2).txt"
#echo $r

foreach($i in $r){
   #echo $i
}

$te = "Hello `r"   $r

echo $te

$te | Out-File "C:\\Users\\UReddy\\Desktop\\travelling - Copy (3).txt"

Input file:

https://www.youtube.com/watch?v=_77nz20nIXo
https://www.youtube.com/watch?v=m55EtpNqpDY&t=196s

Output File:

Hello 
https://www.youtube.com/watch?v=_77nz20nIXo https://www.youtube.com/watch?v=m55EtpNqpDY&t=196s

How to retain the original line endings in the file?

CodePudding user response:

You have join the elements in $r with newline.

$te = "Hello `n"   ($r -Join "`n")

This is because by default Get-Content returns an array of newline-delimited strings

  • Related