Home ยป PowerShell ยป PowerShell Compare Files Modified Date and Creation Dates

PowerShell Compare Files Modified Date and Creation Dates

The file has attributes like CreationTime, LastWriteTime, Length, etc. In PowerShell to compare files by modified date or creation date, use the PowerShell operators lt and gt. It compares files by date and returns a boolean value if it is lesser than or greater than.

# Get File item
$file1 = Get-ChildItem -Path D:\Ps\powershell-command-alias.png

# Get the specific date
$date1 = (Get-Date).AddDays(-10)

# Compare file modified date with specific date
$file1.LastWriteTime -gt $date1

In the above PowerShell script, the $file1 variable contains the file object. To compare the file modified date with the specific date, we have used the PowerShell operator gt (greater than)

PowerShell Compare file Last modified date to specific date
PowerShell Compare file Last modified date to specific date

In this article, we will discuss how to use the PowerShell operators lt and gt to compare the files by their creation date or modified date.

PowerShell Compare File Dates

The file has the attribute `LastWriteTime` which contains the modified date or last write time when a file was modified.

To compare the two files modified dates, use the below PowerShell script that uses Get-ChildItem to get the file item.

# Get the first file 
$file1 = Get-ChildItem -Path D:\Ps\powershell-command-alias.png 

# Get the second file
$file2 = Get-ChildItem -Path D:\Ps\powershell-compare-dates.png

# Compare the two files with their lastwritetime
$file1.LastWriteTime -gt $file2.LastWriteTime

In the above PowerShell script, the Get-ChildItem cmdlet uses the Path parameter to specify the file path and get the item.

Comparing two files modified date in PowerShell requires the file attribute LastWriteTime of each file to compare against the other file using the PowerShell operator lt or gt.

PowerShell operator on comparison of two files last modified date returns a boolean value.

The output of the above script to compare files based on the modified date are:

PS C:\Windows>$file1 = Get-ChildItem -Path D:\Ps\powershell-command-alias.png 

PS C:\Windows>$file2 = Get-ChildItem -Path D:\Ps\powershell-compare-dates.png

PS C:\Windows> $file1.LastWriteTime -lt $file2.LastAccessTime                                                           True

PS C:\Windows> $file1.LastWriteTime -gt $file2.LastWriteTime                                                            False

PS C:\Windows>   

 

Cool Tip: How to get file attributes using the Get-ChildItem in PowerShell!

Compare File Creation Date in PowerShell

The file has attribute creationTime that specifies when the file was created. In PowerShell to compare the creation date with a specific date, use the PowerShell operator lt or gt.

# Get the file item
$file1 = Get-ChildItem -Path D:\Ps\powershell-command-alias.png

# Get the specific date
$cutoffDate = (Get-Date "15-01-2023")   

# Compare the file creation date
$file1.CreationTime -gt $cutoffDate

In the above PowerShell script, the Get-ChildItem cmdlet gets the file item from the path specified and stores it in the $file1 variable.

$cutoffDate variable stores the specific date.

To compare the file creation date with the specific date, use the file creationTime attribute and PowerShell operator gt (greater than) to compare against the other specific date.

The output of the above script after comparing the file creation date is:

PowerShell Compare File Creation Date
PowerShell Compare File Creation Date

Cool Tip: How to find files by last modified date in PowerShell!

Conclusion

I hope the above article on how to compare file modified date with a specific date and creation date is helpful to you.

The file attributes like LastWriteTime are used to get modified DateTime and CreationTime attribute is used to get file creation DateTime.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

How to Convert String to DateTime in PowerShell

PowerShell Epoch Time and DateTime Conversion

PowerShell Get-Date Minus 1 day

PowerShell Compare Dates

Leave a Comment