Powershell Get-ChildItem most recent file in directory

Powershell

Powershell Problem Overview


We produce files with date in the name. (* below is the wildcard for the date) I want to grab the last file and the folder that contains the file also has a date(month only) in its title.

I am using PowerShell and I am scheduling it to run each day. Here is the script so far:

  $LastFile = *_DailyFile
  $compareDate = (Get-Date).AddDays(-1)
  $LastFileCaptured = Get-ChildItem -Recurse | Where-Object {$LastFile.LastWriteTime        
         -ge $compareDate}

Powershell Solutions


Solution 1 - Powershell

If you want the latest file in the directory and you are using only the LastWriteTime to determine the latest file, you can do something like below:

gci path | sort LastWriteTime | select -last 1

On the other hand, if you want to only rely on the names that have the dates in them, you should be able to something similar

gci path | select -last 1

Also, if there are directories in the directory, you might want to add a ?{-not $_.PsIsContainer}

Solution 2 - Powershell

Yes I think this would be quicker.

Get-ChildItem $folder | Sort-Object -Descending -Property LastWriteTime -Top 1 

Solution 3 - Powershell

You could try to sort descending "sort LastWriteTime -Descending" and then "select -first 1." Not sure which one is faster

Solution 4 - Powershell

Try:
$latest = (Get-ChildItem -Attributes !Directory | Sort-Object -Descending -Property LastWriteTime | select -First 1)
$latest_filename = $latest.Name 
Explanation:
PS C:\Temp> Get-ChildItem -Attributes !Directory *.txt | Sort-Object -Descending -Property LastWriteTime | select -First 1


    Directory: C:\Temp


Mode                LastWriteTime         Length Name                                                                                
----                -------------         ------ ----                                                                                
-a----         5/7/2021   5:51 PM           1802 Prison_Mike_autobiography.txt                    
  • Get-ChildItem -Attributes !Directory *.txt or Get-ChildItem or gci : Gets list of files ONLY in current directory. We can give a file extension filter too as needed like *.txt. Reference: gci, Get-ChildItem
  • Sort-Object -Descending -Property LastWriteTime : Sort files by LastWriteTime (modified time) in descending order. Reference
  • select -First 1 : Gets the first/top record. Reference Select-Object / select
Getting file metadata
PS C:\Temp> $latest.Name
Prison_Mike_autobiography.txt

PS C:\Temp> $latest.DirectoryName
C:\Temp

PS C:\Temp> $latest.FullName
C:\Temp\Prison_Mike_autobiography.txt

PS C:\Temp> $latest.CreationTime
Friday, May 7, 2021 5:51:19 PM


PS C:\Temp> $latest.Mode
-a----

Solution 5 - Powershell

@manojlds's answer is probably the best for the scenario where you are only interested in files within a root directory:

\path
     \file1
     \file2
     \file3

However, if the files you are interested are part of a tree of files and directories, such as:

\path
     \file1
     \file2
     \dir1
         \file3
         \dir2
              \file4

To find, recursively, the list of the 10 most recently modified files in Windows, you can run:

PS > $Path = pwd # your root directory
PS > $ChildItems = Get-ChildItem $Path -Recurse -File
PS > $ChildItems | Sort-Object LastWriteTime -Descending | Select-Object -First 10 FullName, LastWriteTime

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser977645View Question on Stackoverflow
Solution 1 - PowershellmanojldsView Answer on Stackoverflow
Solution 2 - PowershellBrian RussellView Answer on Stackoverflow
Solution 3 - PowershellBlevblevView Answer on Stackoverflow
Solution 4 - PowershellKent PawarView Answer on Stackoverflow
Solution 5 - PowershellJesusIniestaView Answer on Stackoverflow