get the latest file in a directory powershell 1

get the latest file in a directory powershell

$dir = "C:\test_code"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name

Here is what the above code is Doing:
1. It’s setting the variable $dir to the path of the folder you want to check.
2. It’s getting a list of all the files in that folder, sorting them by last access time, and then selecting the first one.
3. It’s printing the name of the file.

Similar Posts