Alright I am still really new to PowerShell and i've sifted through a few different threads on here trying to find the best way to go about this. This is the closest I could find to how I need to modify the file. If their is any better ways to go about this I am all ears, like I said still pretty new to powershell.
Goal
- I want to delete the entire group within a text file, then write it to the same file (if possible)
Issue
- I want to delete lines 1 through random number.
- The very first line is always the same within the group, and is always on the same line number.
- The very last line of the group is always the same, but it is never on the same line number.
- I've got the desired output I want, but I am running into issues writing it back to the same file.
- I've tried -replace and set-content but I run into errors at this portion.
Example of the text files within the folder.
I want this deleted
here
here
here
here
To here
but i want this kept in file 1
File 2
I want this deleted
here
here
here
here
here
here
To here
but i want this kept in file 2
Below is what I have come up with.
$files = Get-ChildItem
Foreach-Object {
#Targeting the specific file
$keep = $true
Get-Content $files | Where-Object {
if ( $_.StartsWith("I want this deleted")) {
$keep = $false
} elseif ( -not $keep -and $_.StartsWith("but") ) {
$keep = $true
}
$keep
} #End of where Object
}#End for Foreach loop
Output
but i want this kept in file 1
but i want this kept in file 2