Powershell listing files with different creationtime and lastwritetime. RRS feed

  • Domanda

  • 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!

    • Modificato FTLY venerdì 24 maggio 2019 06:47
    venerdì 24 maggio 2019 06:46

Tutte le risposte

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


    venerdì 24 maggio 2019 07:17
  • 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


    venerdì 24 maggio 2019 07:46
  • That is not even close to what I posted.


    venerdì 24 maggio 2019 07:48
  • 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?

    venerdì 24 maggio 2019 07:58
  • or this:

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


    venerdì 24 maggio 2019 08:07
  • 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.

    venerdì 24 maggio 2019 08:40
  • 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:

    Best regards,


    Just do it.

    lunedì 27 maggio 2019 07:36