0

I'm trying to delete rows from csv base off certain conditions in two columns. Status column if it has T and the second column is Term Date if the date is older then the past 5 days.

little info column STATUS has either "A" or "T".

$path = "data.csv"
$maxAge = (Get-Date).AddDays(-5).Date
Import-Csv $path | Where-Object {$_.STATUS -eq "A" -or $_.TERMDATE -gt $maxAge} | 
Export-Csv -Path temp.csv -NoTypeInformation

it works to a certain degree, but still shows dates from last year.

1
  • Use [datetime]$_.TERMDATE -gt $maxAge in your condition. $_.TERMDATE is going to be a string when read from CSV. It is likely not accurate for your comparison. Commented Aug 4, 2020 at 20:15

1 Answer 1

1

You will need to cast the TERMDATE value to a datetime object in order to make an accurate comparison.

$path = "data.csv"
$maxAge = (Get-Date).AddDays(-5).Date
Import-Csv $path |
    Where-Object {$_.STATUS -eq "A" -or [datetime]$_.TERMDATE -gt $maxAge} |
        Export-Csv -Path temp.csv -NoTypeInformation

If the [datetime] cast does not work, then you will need to parse the string and perform a conversion. In order to provide a solution for that, we need to know the original date format of TERMDATE.

Sign up to request clarification or add additional context in comments.

1 Comment

worked thank you. how would i go about counting work days only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.