# Filter CSV-file

**URL:** <https://forums.powershell.org/t/filter-csv-file/13452>\
**Category:** PowerShell Help\
**Created:** [November 21, 2019, 5:03am UTC](https://forums.powershell.org/t/filter-csv-file/13452 "2019-11-21T05:03:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobodobo](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bobodobo](https://forums.powershell.org/u/bobodobo)\
**Post date:** [November 21, 2019, 5:03am UTC](https://forums.powershell.org/t/filter-csv-file/13452/1 "2019-11-21T05:03:19Z")

</div>

Hi,

I have a CSV-file where I need to filter the column lastdayofwork. I want to keep all rows with a null value or dates that have not expired 31 days from today.

[pre]$date = get-date (Get-Date).AddDays(-31) -Format MM/dd/yyyy  
$file = Import-Csv C:\temp\file.csv -Encoding default | Where-Object{(!$_.lastdayofwork) -or ($_.lastdayofwork -ge $date)}[/pre]

&nbsp;

| Company | Name | lastdayofwork | mail |
| Banana Company | John Smith | | [John.smith@company.com](mailto:John.smith@company.com) |
| Pear Company | Jane Smith | | [jane.smith@company.com](mailto:jane.smith@company.com) |
| Banana Company | Boris Jeltsin | | [boris.jeltsin@company.com](mailto:boris.jeltsin@company.com) |
| Banana Company | Papa Boy | 20/9/2019 | [papa.boy@company.com](mailto:papa.boy@company.com) |
| Banana Company | Peppa Pig | 22/6/2018 | [Peppa.pig@company.com](mailto:Peppa.pig@company.com) |
| Pear Company | Mr Tamagotchi | 9/12/2019 | [Mr.tamagotchi@company.com](mailto:Mr.tamagotchi@company.com) |
| | | | |

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [November 21, 2019, 7:39am UTC](https://forums.powershell.org/t/filter-csv-file/13452/2 "2019-11-21T07:39:20Z")

</div>

Try this, it works…

[pre]

$date = get-date (Get-Date).AddDays(-31) -Format MM/dd/yyyy

Import-Csv -Path C:\temp\file.csv -Encoding ascii | Where-Object {[string]::IsNullOrEmpty($\_.lastdayofwork) -or $\_.lastdayofwork -ge $date}
[/pre]

---

<div class="post-metadata">

**Author:** ![bobodobo](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bobodobo](https://forums.powershell.org/u/bobodobo)\
**Post date:** [November 21, 2019, 8:22am UTC](https://forums.powershell.org/t/filter-csv-file/13452/3 "2019-11-21T08:22:32Z")

</div>

Hi Kiran,

Thanks for your reply. When I run your modified code I get all rows except one

| Banana Company | Papa Boy | 20/9/2019 | [papa.boy@company.com](mailto:papa.boy@company.com) | | | | | | | | | |

&nbsp;

[pre]

Name lastdayofwork

* * *

Name lastdayofwork

* * *

John Smith  
Jane Smith  
Boris Jeltsin  
Peppa Pig 22/6/2018  
Mr Tamagotchi 9/12/2019 [/pre]

&nbsp;

and when I Remove the **_-or $\_.lastdayofwork -ge $date_** I recieved only the rows with null value.

&nbsp;

[pre]Name lastdayofwork

* * *

John Smith  
Jane Smith  
Boris Jeltsin [/pre]

&nbsp;

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [November 21, 2019, 9:07am UTC](https://forums.powershell.org/t/filter-csv-file/13452/4 "2019-11-21T09:07:32Z")

</div>

You’re probably going to need to parse the date into a properly comparable format, I think. Date strings can be compared, but you may not get reliable results from comparing the strings.

> <https://gist.github.com/vexx32/fd286d7fa5ef43a815183d56ceb04c69>

I’m not sure on the latter condition as it’s not super clear exactly what you’re getting at with the expiry, but the parsing will get you a date object you can use and compare in ways that are consistent and not as prone to error. 🙂

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [November 21, 2019, 9:31am UTC](https://forums.powershell.org/t/filter-csv-file/13452/5 "2019-11-21T09:31:46Z")

</div>

Another method similar to Joel’s code, but this uses a calculated expression to do the parse and then you can do date filters to your hearts content

```
$date = (Get-Date).AddDays(-31)

$file = Import-Csv C:\temp\file.csv | 
        Select Company, 
               Name, 
               @{Name='LastDayOfWork';Expression={[datetime]::ParseExact($_.LastDayOfWork, "MM/dd/yyyy", [cultureinfo]::CurrentCulture)}}, 
               Mail

$theseAreTheUsersYoureLookingFor = $file | Where-Object {[string]::IsNullOrWhitespace($_.LastDayOfWork) -or $_.LastDayOfWork -lt $date}

$theseAreTheUsersYoureLookingFor
```

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [November 21, 2019, 10:38am UTC](https://forums.powershell.org/t/filter-csv-file/13452/6 "2019-11-21T10:38:07Z")

</div>

> [@](#):
>
> When I run your modified code I get all rows except one

Exactly, as [Joel /u/ta11ow @vexx32](https://powershell.org/profile/ta11ow/) mentioned you need to parse the date value. Thank you.

---

<div class="post-metadata">

**Author:** ![bobodobo](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bobodobo](https://forums.powershell.org/u/bobodobo)\
**Post date:** [November 22, 2019, 9:44am UTC](https://forums.powershell.org/t/filter-csv-file/13452/7 "2019-11-22T09:44:54Z")

</div>

Hi,

I found a problem with my lastdayofwork column. All dates used slash instead of dash so I replaced them and then used Rob Simmers code and its works. I haven’t tried the other solutions yet.

Did I do something wrong with the format or is slash unsupported in the date format?

Thanks everyone for the help and I really appreciate it!

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/filter-csv-file/13452/8 "2024-05-16T20:31:00Z")

</div>


