Finding Most Recent File by name

FizzySoda,
Welcome to the forum. :wave:t4:

If the file names consist exclusively of 14 numbers with a trailing period you could shorten the filter like this:

Where-Object {$_.BaseName -match '^\d{14}\.$'}

And while it works for PowerShell version 7.x it is best practice to have the pipe symbol actually at the end of a line right before the line break. :wink:

Edit:

I just noticed now …

If that’s the condition you won’t get the desired result with

The parameter -Directory limits the output to folders - not files. :wink:

Here’s how I would do it:

Get-Childitem -Path C:\Files\Archive -File |
Where-Object { $_.BaseName -match '^\d14\.$' } |
Sort-Object -Property BaseName |
Select-Object -First 1
1 Like