How do I find the newest file in a directory using a PowerShell script?

后端 未结 5 1518
迷失自我
迷失自我 2020-12-05 04:40

If, for example, I have a directory which contains the following files:

Test-20120626-1023.txt
Test-20120626-0710.txt
Test-20120626-2202.txt
Test-20120626-19         


        
5条回答
  •  天命终不由人
    2020-12-05 05:07

    If, for some reason, the files creation date is different than the one stamped in the file name then you can parse the file name into a datetime object and sort by expression:

    $file = Get-ChildItem | 
    Sort-Object { [DateTime]::ParseExact($_.BaseName,'\Te\s\t\-yyyyMMdd\-HHmm',$null) } |
    Select-Object -Last 1
    

提交回复
热议问题