How To Get Newest File In Directory In PowerShell?

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.FullName

This 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.

Get Newest File In Directory In PowerShell

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.

How To Get Newest File In Directory In PowerShell

Conclusion

In this PowerShell tutorial, I have explained how to get the newest file in a directory in PowerShell by using various methods like:

  1. Using Get-ChildItem and Sort-Object
  2. Using ForEach-Object for Custom Sorting
  3. Using Get-Item with Sort-Object

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.