Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a powershell script that gets the file with the most recent last write time from a folder?

Tags:

powershell

The subject line says it all. I'd also like to do this using pipes.

I figured that I could use Get-ChildItem, Measure-Object and Where-Object, but Measure-Object doesn't like dates.

Should I have a script block which loops through each item returned from Get-ChildItem and does a comparison to see if it's the most recent? I thought that there should be a handy PS cmdlet for that.

like image 269
Tola Odejayi Avatar asked Dec 07 '25 06:12

Tola Odejayi


1 Answers

Get-ChildItem | Sort LastWriteTime -Descending | Select -First 1
like image 141
Josh Avatar answered Dec 08 '25 20:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!