I have code to find files containing specific words that were written to the folder today. The following code works as expected:
##Define the directory to search
$Crmrpts = '\\1.1.1\crmrpts'
##Define the report names to search for. Full names not wildcard
$Values =
@(
'Customer',
'Product',
'Sales',
'Returns',
'Inventory'
)
##Gci the folder defined above looking for file names matching the reports to search for above
$Files = gci $Crmrpts '*.csv' | Where-Object {
$v = $_.Name
$t = $_.LastWriteTime.Date
$Values | Where-Object { $v.Contains($_) -and $t.Equals((Get-Date).Date) }
}
The important variable here is $t
and the section $t.Equals((Get-Date).Date)
Since I am looking for LastWriteTime.Date specifically, I assume I can change my search criteria to be a date, like so:
$Values | Where-Object { $v.Contains($_) -and $t.Equals("12/13/2020")
But this doesn't work. However, if I run another piece of independent code using the same components, it does work:
gci '\\1.1.1\crmrpts' | ? {$_.Name -like '*Customers*' -and $_.LastWriteTime.Date -eq "12/13/2020"}
Why does the above line work, containing $_.LastWriteTime.Date -eq "12/13/2020"
but that same piece of logic does not work in my original script above?
The only way I can get my original script to work for a date other than current date is like this:
$Values | Where-Object { $v.Contains($_) -and $t.Equals((Get-Date).Date.AddDays(-1))