Home ยป PowerShell ยป PowerShell – Get the Last Modified File in the Directory

PowerShell – Get the Last Modified File in the Directory

To get the last modified file in the directory using PowerShell, use the Get-ChildItem cmdlet to get the items from the directory and use the LastWriteTime or CreationTime property.

The Get-ChildItem cmdlet in PowerShell gets one or more items from the specified location. Its output is passed to another cmdlet to use the LastWriteTime property to get the most recent file in the directory.

In this article, we will discuss how to get the last modified file in the directory using the Get-ChildItem cmdlet and its properties LastWriteTime or CreationTime.

PowerShell โ€“ Get the Last Modified file

Use the Get-ChildItem cmdlet in PowerShell to get the files from the folder and sort the results object by the LastWriteTime property to get the latest file in the directory.

 Get-ChildItem D:\LogTest\FTP-02\ -File | Sort-Object LastWriteTime -Descending| Select-Object -First 1

In the above PowerShell script, the Get-ChildITem cmdlet takes the folder path with a File filter to retrieve only files. It then pass output to Sort-Object cmdlet which uses LastWriteTime property.

We have used Descending property to get the recent file in the directory and Select-Object -First 1 returns the latest file in the directory.

LastWriteTime: This property gets or sets the timestamp when the current file or directory was last written.

The output of the above PowerShell script to get the last modified file in the directory is:

PowerShell - Get Last Modified File in the Directory
PowerShell โ€“ Get the Last Modified File in the Directory

Get the Lastest file in the Directory using PowerShell

To get the latest file in the directory using PowerShell, use the Get-ChildItem cmdlet to get files from the specified folder and use the CreationTime property to get the most recent file created in the directory.

Get-ChildItem D:\LogTest\FTP-02\ -File | Sort-Object -Property -CreationTime | Select-Object -First 1  

In the above PowerShell script, the Get-ChildItem takes the folder path as input and uses -File filter to retrieve only files.

It then passed the output to the Sort-Object cmdlet which uses the CreationTime property to list LastWriteTime for all the files in the folder and select the most latest file in the folder.

The output of the above PowerShell script is:

PS D:\> Get-ChildItem D:\LogTest\FTP-02\ -File | Sort-Object -Property -CreationTime                                    

    Directory: D:\LogTest\FTP-02


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       21-06-2022     15:39             19 recent-file.txt
-a----       02-09-2021     11:32           1338 srtp.py
-a----       02-09-2021     11:32           1338 get-log.py
-a----       22-08-2021     21:55              0 Newline-FileTest.txt


PS D:\> Get-ChildItem D:\LogTest\FTP-02\ -File | Sort-Object -Property -CreationTime | Select-Object -First 1           

    Directory: D:\LogTest\FTP-02


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       21-06-2022     15:39             19 recent-file.txt


PS D:\> 

Cool Tip: How to get file owner using PowerShell!

Conclusion

I hope the above article on how to get the last modified file in the directory using the PowerShell script is helpful to you.

Use the Get-ChildItem cmdlet in PowerShell and LastWriteTime or CreationTime properties to find the latest file in the directory.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment