How can I add a line of text at a specific line Number in a text file?

  Kiến thức lập trình

I have the following Powershell script to search a string pattern in all files in a folder. If the pattern does not exist then it adds the string as expected but at the end of the file. I would like to add the string to a specific line number after line 106. This is the same for all .CONFIG files in the folder.
How can I add this to my existing script as I have tried

$fileContent[106] = "{0}`r`n{1}" -f $appID, $fileContent[106] 

and is returning NULL errors.

With the below working what can I incorporate to add the string at a specific line number?

$files = Get-ChildItem "C:test" -Filter *.config 
$appID = '        <add key="appID" value="1234567890"/>"'

for ($i=0; $i -lt $files.Count; $i++) 
{ 
     $filename = $files[$i].FullName        
     $b =  Select-String -Quiet -Pattern $appID -Path $fileName
        if (-not $b)
      {
       Add-Content -Path $fileName -Value $appID
      }
}

New contributor

John Pipolo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT