Reputation: 65
I am writing a script that checks, recursively, all folders and file names in a directory, and then returns the names and last write times to a text file. I only want the names of files and folders that have been added within the past twenty four hours.
$date = Get-Date
Get-ChildItem 'R:\Destination\Path' -recurse |
Where-Object { $_.LastWriteTime -lt $date -gt $date.AddDays(-1) } |
Select LastWriteTime, Name > 'C:\Destination\Path\LastWriteTime.txt' |
Clear-Host
Invoke-Item 'C:\Destination\Path\LastWriteTime.txt'
The .txt file that is invoked is blank, which, based on the test conditions I have set up should not be the case. What am I doing wrong?
Upvotes: 1
Views: 458
Reputation: 12443
You are missing a logical and. Change:
Where-Object { $_.LastWriteTime -lt $date -gt $date.AddDays(-1) }
to
Where-Object { $_.LastWriteTime -lt $date -and $_.LastWriteTime -gt $date.AddDays(-1) }
Even better to use parenthesis, if you would have used them then the syntax would not have been parsed with the missing AND:
Where-Object { ($_.LastWriteTime -lt $date) -and ($_.LastWriteTime -gt $date.AddDays(-1)) }
Upvotes: 4