Thursday, 19 May 2022

Powershell Get the most recent file in Directory

https://www.thecodebuzz.com/powershell-get-most-recent-file-in-directory-folder/

Powershell Get the most recent file in Directory

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

GetChildItemPowershellGetmostrecentfileinDirectorywithfileextensionusingparameters

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

GetChildItemPowershellGetmostrecentfileinDirectorywithfileextension

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

powershellscripttofindthelatestfileandcheckitssize

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 

 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive