I need to extract data from a csv file if date matches with today’s date.

Here is a sample CSV file. if the DOB = today’s date, output the entire row.

name,DOB,email
John smith, 4/15/2021,j_smith.yahoo.com
john_Doe,4/16/2021,J_doe@yahoo.com

=======================
here is what I got so far:

$File = “C:\report\users.csv”
$Date = Get-Date -Format “MM/dd/yy”
$Pattern = “$($date)”
Get-Content $File | Select-string -Pattern $Pattern

I’m not getting any matched result. It will work If I replace the $Pattern with the actual date: Get-Content $File | Select-string -Pattern 4/15/2021. but failed using the variable $Pattern. I also verified the $Pattern output matches the value from the DOB column. (4/15/2021).

any ideas on what I’m doing wrong. struggle with this all night.

Thank you!

3 Spice ups

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

If its a CSV why not import it as such? See if the following works

$CSV = Import-Csv -Path "C:\report\users.csv"
$Date = Get-Date
$CSV | Where-Object {$_.DOB -eq $Date.ToString('M/dd/yyyy')}
2 Spice ups

Thank you for replying. the issue was the get-date format i used: $Date = Get-Date -Format “MM/dd/yy” it will output like 04/15/2021 and the data on the file is 4/15/2021 (no leading 0). I noticed it on your reply $CSV | Where-Object {$_.DOB -eq $Date.ToString(‘M/dd/yyyy’)}. thank you. again.

so maybe you need to remove leading zero on day as well “M/d/yyyy”