My client wanted to do some file operations on the newest file in a directory. So, I used various PowerShell methods to get the newest file in a directory in PowerShell.
To get the newest file in a PowerShell directory, you can use the Get-ChildItem cmdlet combined with Sort-Object. For example:
$directoryPath = "C:\MyFolder"
$newestFile = Get-ChildItem -Path $directoryPath -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$newestFile.FullNameThis command retrieves the files in the specified path, sorts them by the last write time in descending order, and outputs the full name of the newest file.
1. Using Get-ChildItem and Sort-Object
The simplest way to get the most recent file in a directory using PowerShell is to use the Get-ChildItem cmdlet combined with Sort-Object. Here’s an example PowerShell script that retrieves the latest file based on the last write time:
$directoryPath = "C:\MyFolder"
$newestFile = Get-ChildItem -Path $directoryPath | Sort-Object LastWriteTime -Descending | Select-Object -First 1
Write-Host "The newest file is: $($newestFile.FullName)"This script lists all the items in the specified directory, sorts them by LastWriteTime in descending order, and selects the first item, which is the newest file.
The screenshot below shows the output after I executed the above PowerShell script using VS code on my laptop.

If you’re looking for the newest file with a specific extension, you can add a filter like the script example below.
It will display the latest text file in the directory using PowerShell.
$directoryPath = "C:\MyFolder"
$fileExtension = "*.txt"
$newestFile = Get-ChildItem -Path $directoryPath -Filter $fileExtension | Sort-Object LastWriteTime -Descending | Select-Object -First 1
Write-Host "The newest text file is: $($newestFile.FullName)"This script is similar to the previous one but includes a -Filter parameter to only consider .txt files.
And if you want to exclude directories and only consider files. You can modify the Get-ChildItem cmdlet with the -File switch:
$directoryPath = "C:\MyFolder"
$newestFile = Get-ChildItem -Path $directoryPath -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1
Write-Host "The newest file is: $($newestFile.FullName)"Check out Get Current Directory in PowerShell
2. Using ForEach-Object for Custom Sorting
In PowerShell, you can also use the ForEach-Object to get the newest file from a directory or folder.
Here’s an example:
$directoryPath = "C:\MyFolder"
$newestFile = Get-ChildItem -Path $directoryPath | ForEach-Object {
$_ | Add-Member -MemberType NoteProperty -Name "SortProperty" -Value $_.LastWriteTime -PassThru
} | Sort-Object SortProperty -Descending | Select-Object -First 1
Write-Host "The newest file is: $($newestFile.FullName)"This script adds a custom property to each file object and sorts by that property.
3. Using Get-Item with Sort-Object
This is the 3rd approach, and here, you can use the Get-Item cmdlet in combination with Sort-Object to get the newest file in a directory using PowerShell.
$directoryPath = "C:\MyFolder\*"
$newestFile = Get-Item $directoryPath | Sort-Object LastWriteTime -Descending | Select-Object -First 1
Write-Host "The newest file is: $($newestFile.FullName)"This approach is similar to using Get-ChildItem, but Get-Item is used to retrieve all items directly.
You can see the output in the screenshot below; I executed the above script.

Conclusion
In this PowerShell tutorial, I have explained how to get the newest file in a directory in PowerShell by using various methods like:
- Using Get-ChildItem and Sort-Object
- Using ForEach-Object for Custom Sorting
- Using Get-Item with Sort-Object
You may also like:
- Delete a File in PowerShell
- Find Strings in Files in PowerShell
- Copy and Rename Files in PowerShell
- Get File Extensions from Filenames in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.