Whats the powershell command for finding files in a folder that do not meet a criteria of date range for the Get-Childitem filter of LastWriteTime.
So, check to see if a directory has files that DO NOT contain any files that have LastWriteTime between 01/10/2012 (1st Oct) to 25/10/2012 (25th Oct).
I want to display the folders that DO NOT have any files that are in that range…that way I know they are old and the whole directory can be deleted.
example of this is:
Folder1 – some files written within October – ignore this whole folder and do not display these in the results
Folder2 – NO file has LastWriteTime written in the month of October and so this folder and files should be displayed.
I know this can be done with Get-ChildItem and I am stuck on the bit below..
Get-ChildItem E:\LogArchive -Recurse | Where-Object{$_.LastWriteTime.......?
Simple solution off top of the head is this:
where
$startDateis01/10/2012 (1st Oct)and$endDateis25/10/2012 (25th Oct).Problem with this approach is that it does not account for time factor in the
[datetime]. So in your example, if you have25-Oct-2012as an upper bound, it will return files created at25-Oct-2012 9:00, which you may not want.Below code calculates
[datetime]‘s with time part truncated, based on the input[datetime], and builds the month-to-date date range of($dayStart, $dayEnd):For this sample code, here is the output:
Notice this approach is flexible, because you can set
$entry_dateto eitherDateorString, and it may or may not have time part – it will work in all those cases. You can then have this code inWhere-Object:Notice how
-ge $dayEnd.AddDays(1)fixes the issue when comparing25-Oct-2012to25-Oct-2012 9:00.