$path = "C:\ProgramData\Datadog\datadog.yaml" $content = Get-Content $path if ($content -match '^\s*remote_configuration:') { $content = $content -replace '(^\s*enabled:\s*).+', '${1}true' } else { $content = $content -replace '(^\s*api_key:.*$)', "`$1`r`nremote_configuration:`r`n enabled: true" } $content | Set-Content $path