Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace of a specific line number

Tags:

powershell

I'm trying to replace line number 11 in a txt file using PowerShell.

Firstly I tried replacing a specific word, but it changed too much:

$output= (Resolve-DnsName -name name1).IPAddress 

(Get-Content "C:\test\test.txt") -replace "IPADDRESS=","IPADDRESS=$output"  | Set-Content C:\test\test.txt
like image 994
mino Avatar asked Oct 27 '25 08:10

mino


1 Answers

If you want to replace something within a certain line, you can use the index operator on the string array that the Get-Content cmdlet returns:

$content = Get-Content "C:\test\test.txt"
$content[10] = -replace "IPADDRESS=","IPADDRESS=$output" 
$content | Set-Content C:\test\test.txt
like image 56
Martin Brandl Avatar answered Oct 29 '25 09:10

Martin Brandl