Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Retrieving the most recent file if, and only if, it was modified within the last hour

I'm using a PowerShell script to retrieve a file from a remote directory. I only want to retrieve the most recent file if it was modified within the last hour. My code snippet is as follows:

$directoryInfo = $session.ListDirectory($remotePath)
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1

I believe that I need to add another condition to the "Where-Object" clause, but I don't know the proper format. For example,

Where-Object { -Not $_.IsDirectory and <created/modified within the last hour> }

How do I do this? Is there a better way?