Share via


Powershell listing files with different creationtime and lastwritetime.

Question

Friday, May 24, 2019 6:46 AM

Goodmorning everyone,

I am trying to create a Powershell script that will list all files in a directory that have not matching CreationTime and LastWriteTime. So far, what I got is the following;

Get-ChildItem -Path C:\MyPath -Recurse |  Where-Object {$_.CreationTime -ne $_.LastWriteTime}

So far this is working as I need it to. However, I want to make a change to this script so it will only list the files that have more then 2 seconds difference in the creationtime and lastwritetime.

Does anyone know how to accomplish this?

Thanks and regards!

All replies (7)

Friday, May 24, 2019 7:17 AM | 1 vote

($_.CreationTime - $_.LastWriteTime) -gt [timespan]'0:0:0:2'

\(ツ)_/


Friday, May 24, 2019 7:46 AM

Thanks for your reply, I did try this but it doesn't give any results, while it should do.

PS C:\Users\Test> Get-ChildItem -Path C:\MyPath -Recurse | Where-Object {$_.CreationTime -ne $_.LastWriteTime} | select name,creationtime,LastWriteTime

Name                         CreationTime       LastWriteTime     
                                    
Doc1.docx                    24-5-2019 08:23:40 24-5-2019 08:23:41
test1.xlsx              24-5-2019 08:18:06 24-5-2019 07:46:40
test2.xlsx              24-5-2019 07:46:40 24-5-2019 08:18:52

PS C:\Users\Test> Get-ChildItem -Path C:\MyPath -Recurse | Where-Object {$_.CreationTime -ne $_.LastWriteTime} -gt [timespan]'0:0:0:2' | select name,creationtime,LastWriteTime

PS C:\Users\Test> Get-ChildItem -Path C:\MyPath -Recurse | Where-Object {$_.LastWriteTime - $_.CreationTime} -gt [timespan]'0:0:0:2' | select name,creationtime,LastWriteTime

Thanks


Friday, May 24, 2019 7:48 AM

That is not even close to what I posted.

\(ツ)_/


Friday, May 24, 2019 7:58 AM

Should it be more like this;

Get-ChildItem -Path C:\MyPath -Recurse | Where-Object ($_.CreationTime - $_.LastWriteTime) -gt [timespan]'0:0:0:2'

Or do I also need to remove the path -recurse and so?


Friday, May 24, 2019 8:07 AM

or this:

Get-ChildItem -Path . -Recurse | 
    Where-Object{[math]::abs(($_.CreationTime - $_.LastWriteTime).TotalSeconds) -lt 2}

\(ツ)_/


Friday, May 24, 2019 8:40 AM

Thanks, this is looking more like what I need. This script however will list the documents that have identically CreationTime and LastWriteTime. Can we turn this around so it will list this files that have non-matchin CreationTime and LastWrite time, with a deviation bigger then 2 seconds.

I tried changing the "-" between CreationTime and LastWriteTime in your script, but that doesn't seem to do anything if I change it with -ne for example.

Thanks again for your help jrv.


Monday, May 27, 2019 7:36 AM

Hi,

 Thanks for your reply.

"-lt": Less than

"-gt": Greater than

Maybe you can use "-gt" to solve your issue.

More information, please refer the link below:

https://4sysops.com/archives/powershell-comparison-operators-eq-lt-gt-contains-like-match

Best regards,

Lee

Just do it.