Powershell Get the most recent file in Directory
Today in this article, we shall see l simple and easy technique of Powershell Get the Latest file in Directory using basic and other filter criteria.
We shall use PowerShell scripts to search the files as per multiple search criteria.
Today in this article, we will cover below aspects,
- Getting Files from a Given Folder Directory using PowerShell
- Getting Files from a given Directory using file extension filter
- Getting a single latest from a given Directory using file extension filter
- PowerShell- Get the most recent file in Directory with a filter
- PowerShell – Get all files from a given Directory with a filter
We shall see how to get all files from the directory or most recent files using multiple filters like adding multiple files Example: .txt or .jpg or .csv etc.
Let’s consider the below folder, for example, I have a series of nested folders with different files which include .xlsx or .png, etc.
Getting Files from a Given Folder Directory using PowerShell
Get all files from a directory
Get all files from a directory or get all files in a loop for loop in PowerShell.
Command
Get-ChildItem
Example
PS C:\Test> Get-ChildItem
Get-ChildItem method returns the names of files or folders in the specified directory i.e C:\Test
You can specify the path using the below commands as well,
PS C:\Test> Get-ChildItem C:\\Test\Test1
Getting Files from a given Directory using file extension filter
Get all files from a directory,
Command
Get-ChildItem -Attributes !Directory *.* | Sort-Object -Descending -Property LastWriteTime
Get all files from a directory with .txt or .xlsx extension only
Command
Get-ChildItem -Attributes !Directory *.txt| Sort-Object -Descending -Property LastWriteTime
Get all files from a directory with .jpeg extension only,
Get-ChildItem -Attributes !Directory *.jpeg | Sort-Object -Descending -Property LastWriteTime
Using Parameter to store the list of Names
The above commands give us a list of all files with the file extension “.xlsx”. We are able to print the file name using $latestfile.Name
Similar way we can easily print different attributes of file names like file name or Last Modified Time or Creation time etc.
PS C:\Test> $latestfile = Get-ChildItem -Attributes !Directory *.png | Sort-Object -Descending -Property LastWriteTime | select -First 1 PS C:\Test> $latestfile.Name TheCodeBuzz.png PS C:\Test> $latestfile.CreationTime Saturday, April 24, 2021 5:46:49 PM PS C:\Test> $latestfile.FullName
Getting a single latest from a given Directory using file extension filter
One can use a combination of properties like LastWriteTime and Select -First 1 to get the latest file in the given directory.
Get a single latest from a given Directory without file filter
Example:
PS C:\Test> $latestfile = Get-ChildItem -Attributes !Directory . | Sort-Object -Descending -Property LastWriteTime | select -First 1 PS C:\Test> $latestfile.Name Book1.xlsx
PowerShell- Get the most recent file in Directory with a filter
Get the most recent file in the Directory with a filter,
PS C:\Test> $latestfile = Get-ChildItem -Attributes !Directory *.png | Sort-Object -Descending -Property LastWriteTime | select -First 1 PS C:\Test> $latestfile.Name TheCodeBuzz.png
PowerShell – Get all files from a given Directory with a filter
If you need to get all the files from the given directory, please use the below logic,
PS C:\Test> $latestfile = Get-ChildItem -Attributes !Directory *.png | Sort-Object -Descending
References:
That’s all! Happy coding!
Does this help you fix your issue?
Do you have any better solutions or suggestions? Please sound off your comments below.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.