Get Last Modified File in Directory on Windows

Get Last Modified File in Directory on Windows

Navigating through a directory and identifying the most recently modified file is a common requirement in various programming scenarios on Windows. Whether you're managing data, automating tasks, or analyzing changes over time, knowing how to efficiently retrieve the last modified file is a crucial skill. This tutorial demonstrates how to get the last modified file in a directory on Windows.

PowerShell is a scripting language on Windows that allows to easily access and manage the file system. To retrieve the last modified file in a specified directory, use the following command:

Get-ChildItem "C:\Windows\System32\drivers" -File | Sort LastWriteTime | Select -Last 1

Let's break down each part of the command to understand its functionality:

  • Get-ChildItem - is a PowerShell cmdlet used to retrieve items (files, directories, etc.) in a specified location.
  • -File - is a parameter that instructs Get-ChildItem to retrieve only files (not directories or other items).
  • Sort LastWriteTime - sorts files in ascending order based on their last write time.
  • Select -Last 1 - selects the last (most recent) item from the sorted list of files.

Leave a Comment

Cancel reply

Your email address will not be published.