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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With