Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6148
  • Last Modified:

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?
  • 2
1 Solution
Raheman M. AbdulSenior 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!

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now