Fler
Fler

Reputation: 392

PowerShell Importing CSV

I'm importing a CSV. I have a column called 'Date of Birth' with dates formatted as 'yyyy/MM/dd' and 'yyyy-MM-dd'.

I'm only looking to pull records where the date is in the latter format (yyyy-MM-dd)

My code below works, but it's not pulling any records back for $SearchFor = "-" and when using $SearchFor = "/" I'm being returned all records.

Import-CSV C:\Test\TestFile.csv | Where-Object { $_."Date Of Birth" -Notlike $SearchFor } | ForEach-Object {

Does anyone know what's going on, and how I can only get the records I'm looking for?

Upvotes: 0

Views: 68

Answers (1)

Freddy Grande
Freddy Grande

Reputation: 111

You should be getting back all records regardless if your query is:

$_."Date Of Birth" -Notlike $SearchFor

Where $SearchFor is either "-" or "/"

You need to add wildcards and use "*-*" and "*/*" otherwise the -like operator behaves similar to the -eq operator.

Better yet, use regex to match the exact formats which seem to be set in stone:

Match dates with dashes: $SearchFor = "^\d{4}-\d{2}-\d{2}$"

$_."Date Of Birth" -match $SearchFor

To match dates with slashes, just slightly edit the regex query string: $SearchFor = "^\d{4}/\d{2}/\d{2}$"

Upvotes: 2

Related Questions