3

I am trying to add multiple line at the end of the file using powershell. for example : The file is already present and have some content in it.

abc.txt

abc
def
ghi
jkl
mno
pqr

So abc.txt file looks like something I have mention above. Now i am trying to add below content at the end of the file and on new line.

qwe
asd
zxc
vbn

The final output will be - abc.txt

abc
def
ghi
jkl
mno
pqr
qwe
asd
zxc
vbn

As i am a newbiw i tried adding one line in a file which has been perform successfully but no idea of adding multiple line.

 Add-Content "./sample3.txt" "This is the last line `n"
Sumeet Singh
  • 61
  • 1
  • 7

1 Answers1

9

You can add content as given below. Read more on here string

$contentToAdd = @"
qwe
asd
zxc
vbn
"@

 Add-Content "C:\dev\abc.txt" $contentToAdd
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
  • Thanks for your comment.The script works perfect but the content get add to leaving one line for eg: if file content end at 29 line then after running the script it adds the content from 31 line . There is a gap of one line.Is there any solution for it.Thanks once again for helping. – Sumeet Singh Jul 05 '21 at 04:03
  • When you put the here string, after "@, put an Enter to insert a new line and keep the content. I tested it. It worked fine for me. – Venkataraman R Jul 05 '21 at 04:15