I want to delete a complete line which contains a special word in a single .csv file in a powershell script.
I already found working code, that deletes the specific line, but writes all other lines into the first line. This shouldn't happen, because I linked the csv-file with an ms access table.
$user = 'User2'
$file = Get-Content c:\datei.txt
$newLine = ""
foreach($line in $file){
if($line -match $User){
}else{
$newLine += $line
}
}
$newLine | Out-File c:\datei.txt
The file looks like this, but with more data and lines:
User;computer;screen1;screen2;printer
User1;bla;bla;;bla
User2;bla;bla;bla;bla
User3;bla;bla;bla;bla
After running the code:
User;computer;screen1;screen2;printerUser1;bla;bla;;blaUser3;bla;bla;bla;bla
I use Powershell 5.1.x on Windows 7