Using '-lt' with get-date

Hello,

quick question regarding the below piece of PS. When the script reaches the ‘-lt’ will it only take the time into consideration or the date too? For example, if the last write was at 13:00 on the 1st of jan and the next at 12:59 on the 2nd, woud the ‘-lt’ check think there was one minute of difference or include the date in it’s calculation?

$TestLastWrite = Get-Item -LiteralPath 'C:\Users\user1\Videos'
$CurrentDateTime = Get-Date

if ($TestLastWrite.LastWriteTime -lt $CurrentDateTime.AddMinutes(-60) ) {
do foo
}

Thanks.

Since you actually call Get-Date and not Get-Time and the resulting object type is [DateTime] … what do you think? :wink:

And it should be actually very easy to be verified with some test files.

But you may pay attention what you query. The LastWriteTime of a folder is determined by the LastWriteTime of the conained files. :point_up_2:t4:

I asked the question to find out what other think. I know what I think.

I’m monitoring the folder for contained file changes, so LastWriteTime is what I want.

But in this case it is not a matter of opinion. It is a fact that an object property of the type [DateTime] contains a certain moment in time. And that consist of a date and a time. :man_shrugging:t4:

Great. :+1:t4: That is often misunderstood and I just wanted to point that out. :slight_smile:

1 Like

I wasn’t sure of the “fact” so I asked a question. What I thought may be wrong. Therefore the answer of “what do you think?” is not helpful.

I was asking about how using ‘-lt’ would use date/time, not what get-date does.

Is your question answered? Do you know now how it works?

If you’re monitoring for changing files look up using a file system watcher.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.