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
1