Something wrong with my IF-ELSE statement and my dumb brain cannot figure out what the heck it is!
If I run the below code on its own it shows each directory and files in there in the below format:
get-childitem E:\LogArchive -recurse | where-object {$_.lastwritetime -gt 60}
Format of output:
Directory: E:\LogArchive\W3SVC100
Mode LastWriteTime Length Name
---- ------------- ------ ----
----- 29/03/2007 15:03 663 ex070329.log.gz
----- 30/03/2007 15:44 860 ex070330.log.gz
----- 03/04/2007 13:41 354 ex070403.log.gz
----- 05/04/2007 14:00 704 ex070405.log.gz
----- 10/04/2007 17:56 921 ex070410.log.gz
----- 11/04/2007 14:55 987 ex070411.log.gz
----- 12/04/2007 15:12 539 ex070412.log.gz
However, when this is run in a code it shows as the below, WITHOUT all the folder structure and dates etc:
W3SVC100
W3SVC102
W3SVC105
W3SVC106
W3SVC1108492480
W3SVC112
W3SVC116
W3SVC118
W3SVC1209046175
W3SVC123110214
W3SVC1262336480
W3SVC127
W3SVC134
W3SVC134239081
W3SVC137
W3SVC139
W3SVC145
W3SVC147
W3SVC1499983181
W3SVC15
How do I get the first results when the below script is run – so show all the modified date, last write time etc . I am currently inputting a message to the user if no files are found in the date range then a message is displayed – however, if it did find anyfiles then display them as listed in the first output type…….
I actually think the fault is on this line but cannot figure out how to amend this:
if ( $runchilditem.lastwritetime -gt DateToCompare)
……In fact – I want to put the output to CSV – any ideas how I can do this?
CODE:
$path = Read-Host "Please enter the path of the folder yu wish to check - this will check sub-folders as well"
Write-Host "`n"
$days = Read-Host "Please enter in number of DAYS you wish to go back"
$DateToCompare = (Get-Date).AddDays(-$days)
$runningtrue = Get-ChildItem $path -Recurse | where-object {$_.lastwritetime -gt $DateToCompare}
Write-Host "`n"
$runchilditem = @(Get-ChildItem $path -Recurse)
if ( $runchilditem.lastwritetime -gt DateToCompare)
{
Write-Host "No Files Matching Date Criteria Found"
}
else
{
$runningtrue
}
If I understand your need, here is how I would do it: