none
Powershell listing files with different creationtime and lastwritetime. RRS feed

  • 问题

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


    • 已编辑 FTLY 2019年5月24日 6:47
    2019年5月24日 6:46

全部回复

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

    \_(ツ)_/

    2019年5月24日 7: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

    Thanks

    2019年5月24日 7:46
  • That is not even close to what I posted.


    \_(ツ)_/

    2019年5月24日 7: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?

    2019年5月24日 7:58
  • or this:

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


    \_(ツ)_/

    2019年5月24日 8: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.

    2019年5月24日 8: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:

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

    Best regards,

    Lee


    Just do it.

    2019年5月27日 7:36
    版主