Tola Odejayi
Tola Odejayi

Reputation: 3059

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

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.

Upvotes: 3

Views: 507

Answers (1)

Josh
Josh

Reputation: 69252

Get-ChildItem | Sort LastWriteTime -Descending | Select -First 1

Upvotes: 6

Related Questions