Solved

Powershell add text to a specific line in txt file

Posted on 2014-02-12
3
5,176 Views
Last Modified: 2014-02-12
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?
0
Comment
Question by:SvenIA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 39853041
$data = get-content c:\test\file.txt
$data[9]="some text here"
$data | out-file c:\test\file.txt
0
 
LVL 7

Author Comment

by:SvenIA
ID: 39853312
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
 
LVL 7

Author Closing Comment

by:SvenIA
ID: 39853327
It does seem to work now. Don't know why it didn't just before. Thanks a lot!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question