How to Get the Last Modified Date of a File in PowerShell?

In this tutorial, I will explain how to get file modified date in PowerShell. As an IT administrator for a USA-based company, I recently faced a situation where I needed to find out when a specific file was last modified. It is easy in PowerShell, let me show you how.

PowerShell Get File Modified Date using the Get-Item Cmdlet

The simplest way to get the file modified date in PowerShell is to use the Get-Item cmdlet followed by the LastWriteTime property. For example, let’s say I have a file called “sales_report.xlsx” located in the “C:\Reports” directory. To find out when this file was last modified, I can run the following command:

Get-Item "C:\Reports\sales_report.xlsx" | Select-Object LastWriteTime

This will output something like:

LastWriteTime
------------
12-11-2024 17:28:37

The LastWriteTime property shows the date and time the file was last modified. I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell get file modified date

The above PowerShell cmdlet displays the output in date and time format. But sometimes you might need to just get the file modified date. Here is how to only the last modified date in PowerShell using formatting options.

Format the Output

To format the output and display only the date, you can use the ToString() method with a specific format string. Here is the PowerShell cmdlet.

(Get-Item "C:\Reports\sales_report.xlsx").LastWriteTime.ToString("MM/dd/yyyy")

This will display the modified date of the “ProjectProposal.docx” file in the format “MM/dd/yyyy”.

Here is the exact output in the screenshot below:

powershell get file last modified date

Check out PowerShell unblock-file

Get Multiple Files Modified Date in PowerShell

I will show you here how to get the modified date of multiple files in PowerShell.

If I need to check the modified dates of multiple files in a directory, I can use the Get-ChildItem cmdlet instead. For instance, to get the last modified dates of all Excel files in the “C:\Reports” folder:

Get-ChildItem "C:\Reports\*.xlsx" | Select-Object Name, LastWriteTime

This command will list the filenames and last modified timestamps for each Excel file found, like:

Name                     LastWriteTime
----                     -------------
MyExcelFile.xlsx         12-08-2024 17:01:16
sales_report.xlsx        12-11-2024 17:28:37
Test Excel_20240714.xlsx 11-06-2024 10:47:19

Here is the exact output in the screenshot below:

powershell file modified date

Read PowerShell Create Log File

Automate File Modified Date Checks in PowerShell

To automate the process of checking file modified dates and perhaps sending notifications if files are updated, you can set up a scheduled task in Windows Task Scheduler that runs a PowerShell script periodically.

Example: Send an Email Notification

Below is a script that checks if a file has been modified and sends an email notification if it has.

$directory = "C:\Reports"
$cutoffDate = Get-Date "2024-11-01"
$files = Get-ChildItem -Path $directory | Where-Object { $_.LastWriteTime -gt $cutoffDate }

if ($files) {
    $smtpServer = "smtp.your-email-provider.com"
    $smtpFrom = "admin@yourdomain.com"
    $smtpTo = "recipient@domain.com"
    $messageSubject = "Files Modified After $cutoffDate"
    $messageBody = "The following files have been modified after $cutoffDate:`n`n" + ($files | Format-Table -AutoSize | Out-String)

    $smtpMessage = New-Object System.Net.Mail.MailMessage($smtpFrom, $smtpTo, $messageSubject, $messageBody)
    $smtpClient = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtpClient.Send($smtpMessage)
}

This script checks for files modified after a certain date and sends an email with the list of modified files.

Conclusion

In this tutorial, I explained how to get the file last modified date in PowerShell using Get-Item Cmdlet’s LastWriteTime property with some examples. I also show how to get the last modified date of multiple files in PowerShell. Do let me know in the comment below if this tutorial helps you.

You may also like:

>
Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…