Powershell · 2015-12-01

Powershell – Finding the most recent backup file in a parent directory

Having to sift through a large directory to get the latest file can be tedious.  Surprisingly, I’ve needed this twice in the past month, so I figured I should post it.  I hope this helps someone.

cls
cd \\PARENT_DIRECTORY_NAME
$FormatEnumerationLimit = -1
# Get the list of child items that will be searched
$dirs = Get-ChildItem|Select FullName

foreach($dir in $dirs)
{
$dir = $dir.FullName
Get-ChildItem -Path $dir|WHERE-OBJECT {$_.Name -like "*.bak"} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 FullName, LastWriteTime;
}