Background info I need to remove a specific line within a file based on if the line below it contains x text
eg: if the below exists then delete the line above it
In a previous post I was just deleting certain line numbers but have found they differ for some users…this now makes it more complex hence a new topic.
This looks like json to me. If so, there is a built in conversion command in PowerShell that will greatly simplify your life. Just import the file, do the conversion, modify using normal objects (not parsing text), dump it back out to a new file.
$myfile = "C:\yourfile.txt"
$data = Get-Content -Raw -Path $myfile | ConvertFrom-Json
#modify data as needed here
$data | ConvertTo-Json | Set-Content -Path "C:\Yournewfile.txt"
This is why you need to work with a collection of objects not parsing strings with regex. Can you post an entire file contents (if you need to redact personal info just ‘xxx’ through it? If so, I’ll show you what I mean.