Powershell add text to a specific line in txt file


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?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

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
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.
SvenIAAuthor Commented:
It does seem to work now. Don't know why it didn't just before. Thanks a lot!
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.