powershell - delete row in csv file
-
Hi again,
I have a script that will loop through (ForEach-loop) the csv file. The script will test some criteria and if it is a match it will execute the command. Afterward, I want the script to delete that row as it is no longer valid. However, any other row that does not match the criteria should be left alone.This is what I have so far...
$CsvPath = "E:\Docs\Documents\Scripts\OOO.csv"
$CsvImport = Import-Csv $CsvPath
$CsvDate = Get-Date -Format 'M/d/yyy'
$CsvTime = Get-Date -Format 'hh:mm tt'
foreach ($CsvItem in $CsvImport)
{
$CsvEmailFrom = $csvItem.emailfrom
$CsvEmailTo = $csvitem.emailto
$CsvStartDate = [datetime]$CsvItem.startdate
$CsvStartDate = $CsvStartDate.ToShortDateString()
$CsvStartTime = [datetime]$CsvItem.starttime
$CsvStartTime = $CsvStartTime.ToString('hh:mm tt')
$CsvEndDate = [datetime]$CsvItem.enddate
$CsvEndDate = $CsvEndDate.ToString('hh:mm tt')
$CsvEndTime = [datetime]$CsvItem.endtime
$CsvEndTime = $CsvEndTime.ToString('hh:mm tt')
$customTime = [datetime]"09:59AM"
$customTime = $customTime.ToString('hh:mm tt')
#Invalid start/enddate
#($CsvStartDate -gt $CsvEndDate)if ($CsvDate -eq $CsvStartDate -and $CsvDate -le $CsvEndDate){
if ($CsvTime -gt $CsvStartTime){
$CsvStartDate.Remove($CsvStartDate)
Export-Csv $CsvPath
}}
}The table is a simple table
Edit: I did found couple ways to do this, but the script they provided is a lot more complicate than I like it to be. I am trying to avoid my future self from going crazy trying to understand WTF my past self has wrote.
-
did you looked at this?
it uses the Remove method and seems quite straightforward to me. You just need to rework the for loop or simply add a counter to it to know which row has to be purged.