How to find a String in a file which was lastly changed on a specific date?

Viewed 29

The following piece of code works, but only for today's date. What do I have to change to put in a specific date, e.g. the 3rd of May 2022? I also want to do that with a Read-Host.

Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse | 
    Where-Object LastWriteTime -ge ([datetime]::Today) | 
    Sort-Object LastWriteTime | 
    Select-String -Pattern Test
1 Answers
(get-item "C:\tmp\test.xml").lastwritetime.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

The datatype is DateTime, so no need to build matching strings.

In regards to the requirement to use read-host you could do:

[datetime]$DateTime = read-host

By doing so you cast the Input as DateTime but this requires that you enter a supported format. I think its easier if you have to specify the days back to calculate the DateTime information, e.g.;

[int]$daysBack = read-host
Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse | 
    Where-Object LastWriteTime -ge (get-date).AddDays(-$daysback) | 
    Sort-Object LastWriteTime | 
    Select-String -Pattern Test
Related