简体   繁体   中英

How to find second latest ( accessed) file in Windows folder using powershell

I have a folder containing 5 files.

I want to skip the first latest file and want the name of second latest file. How I can do this using PowerShell commands?

Do this:

Get-ChildItem -File | Sort-Object LastAccessTime -Descending | Select-Object -Skip 1 -First 1 -ExpandProperty name

This uses Get-ChildItem to return just files.

It then uses the Sort-Object cmdlet to sort them by their LastAccessTime value in Descending order (so the most recently accessed is at the top).

It then uses Select-Object to skip the 1st item in the collection (the most recently accessed) and return just the first result of the remaining items (ie the second most recently accessed).

Finally the -ExpandProperty parameter of Select-Object is used to return just the Name property as its string type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM