How to Get File Modified Date in PowerShell?

If you want to know how to get file modified date in PowerShell, check out this tutorial completely. In this blog post, we will explore various methods to retrieve file modification dates using PowerShell.

To get the file modified date in PowerShell, you can use the Get-Item cmdlet followed by the LastWriteTime property. For example, $file = Get-Item ‘C:\MyFolder\file.txt’; $file.LastWriteTime will return the last modified date and time for the specified file. For multiple files, use Get-ChildItem and iterate through the results to access their LastWriteTime properties.

Get File Modified Date in PowerShell Using Get-Item Cmdlet

The Get-Item cmdlet in PowerShell is one of the simplest ways to retrieve the last modified date of a single file. It provides information about the file object, including its properties, such as LastWriteTime, which indicates the last time the file was written to.

Here is the complete PowerShell script.

$file = Get-Item "C:\MyFolder\MyFile.txt"
$lastModified = $file.LastWriteTime
$lastModified

This script assigns the file’s properties to the $file variable and then extracts the LastWriteTime property, storing it in the $lastModified variable.

You can see the screenshot below after I executed the script using VS code.

Get File Modified Date in PowerShell

Get All Files Modified Date in PowerShell using Get-ChildItem Cmdlet

To retrieve the last modified dates of multiple files within a directory, the Get-ChildItem cmdlet in PowerShell. This cmdlet can be used to list items in one or more specified locations. If you want to see the last modified date for all files in a directory, you can use the following PowerShell script:

$files = Get-ChildItem "C:\MyFolder"
foreach ($file in $files) {
    $lastModified = $file.LastWriteTime
    Write-Host "$($file.Name) was last modified on $lastModified"
}

This PowerShell script will iterate through each file in the specified directory, outputting the name of the file and its last modified date to the console.

Check out the screenshot below:

Get All Files Modified Date in PowerShell using Get-ChildItem Cmdlet

Format the Date Output

Sometimes, you might want to format the date output to a specific format. PowerShell makes this easy with the ToString() method, which can be applied to date objects.

$file = Get-Item "C:\MyFolder\file.txt"
$lastModified = $file.LastWriteTime.ToString("MM/dd/yyyy HH:mm:ss")

This example will format the last modified date to display in the format of month/day/year followed by the hour:minute:second.

Filter Files by Modification Date in PowerShell

If you’re looking for files modified within a certain date range, you can combine Get-ChildItem with the Where-Object cmdlet to filter the results.

Here is the PowerShell script that will provide list of files that have been modified in the last 30 days from the current date.

$dateCutoff = (Get-Date).AddDays(-30) # Files modified in the last 30 days
$files = Get-ChildItem "C:\path\to\directory" | Where-Object { $_.LastWriteTime -gt $dateCutoff }

Get Folder Last Modified Date in PowerShell

If you want to include directories in your search and retrieve their last modified date, you can modify the Get-ChildItem cmdlet to include the -Directory parameter.

Here is the PowerShell script.

$directories = Get-ChildItem "C:\MyFolder" -Directory
foreach ($dir in $directories) {
    $lastModified = $dir.LastWriteTime
    Write-Host "$($dir.Name) was last modified on $lastModified"
}

This script will output the name and last modified date of each directory within the specified path.

Conclusion

PowerShell provides multiple ways to retrieve file modification dates. In this tutorial, I have explained how to get file modified date in PowerShell. The easiest method is to use the Get-Item Cmdlet in PowerShell.

You may also like: