Powershell add text to a specific line in txt file

Hi!

I have a text file. I need to add some text at line 10 of this file. Line 10 is empty, so 'replace' is not an option.

Can it be done using powershell?
LVL 7
SvenIAAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Raheman M. AbdulConnect With a Mentor Senior Infrastructure Support Analyst & Systems DeveloperCommented:
$data = get-content c:\test\file.txt
$data[9]="some text here"
$data | out-file c:\test\file.txt
0
 
SvenIAAuthor Commented:
Thanks, but with this code i cannot add text on an empty line, only overwrite the text on the previous line.

Array assignment failed because index '10' was out of range.
0
 
SvenIAAuthor Commented:
It does seem to work now. Don't know why it didn't just before. Thanks a lot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.