Usando cmdlets nativos do PowerShell:
# Set file name
$File = '.\nsclient.ini'
# Process lines of text from file and assign result to $NewContent variable
$NewContent = Get-Content -Path $File |
ForEach-Object {
# If line matches regex
if($_ -match ('^' + [regex]::Escape('[/settings/external scripts/scripts]')))
{
# Output this line to pipeline
$_
# And output additional line right after it
'Your new line goes here'
}
else # If line doesn't matches regex
{
# Output this line to pipeline
$_
}
}
# Write content of $NewContent varibale back to file
$NewContent | Out-File -FilePath $File -Encoding Default -Force
Usando métodos estáticos .Net ( ReadAllLines \ WriteAllLines ):
# Set file name
$File = '.\nsclient.ini'
# Process lines of text from file and assign result to $NewContent variable
$NewContent = [System.IO.File]::ReadAllLines($File) |
ForEach-Object {
# If line matches regex
if($_ -match ('^' + [regex]::Escape('[/settings/external scripts/scripts]')))
{
# Output this line to pipeline
$_
# And output additional line right after it
'Your new line goes here'
}
else # If line doesn't matches regex
{
# Output this line to pipeline
$_
}
}
# Write content of $NewContent varibale back to file
[System.IO.File]::WriteAllLines($File, $NewContent)