Home » PowerShell » Get-ChildItem LastWriteTime – Find files by Last Modified Date

Get-ChildItem LastWriteTime – Find files by Last Modified Date

Use the Get-ChildItem LastWriteTime attribute to find the list of files with lastwritetime.Get-ChildItem in PowerShell gets one or more child items from the directory and subdirectories.

In this article, we will discuss how to use the Get-ChildItem to get lastwritetime for files, sort files by lastwritetime, and get child items where lastwritetime is greater than the specified date.

Get-ChildItem where lastwritetime greater than 30 days

Use the Get-ChildItem LastWriteTime attribute to get files where lastwritetime is greater than 30 days.

Get-childitem -Path D:\PowerShell\ -recurse | Sort-Object -Property LastAccessTime | Where-Object {$_.lastwritetime -gt (get-date).addDays(-30) -and -not $_.PSIsContainer}   

In the above PowerShell script, Get-ChildItem uses the recurse parameter to get files from directories and subdirectories.

It uses the PSIsContainer property to list files in the directory.

Sort-Object uses the property LastWriteTime to sort the files by ascending order.

Where-Object checks the condition where the file lastwritetime is greater than 30 days and gets only files.

The output of the above script is to find files lastwritetime greater than 30 days and sort files by their lastwritetime file attribute.

Get-ChildItem lastWriteTime greater than 30 days
Get-ChildItem lastWriteTime greater than 30 days

PowerShell Tip: How to search string in files using PowerShell!

Get-ChildItem where lastwritetime today

Use the Get-ChildItem to get files where lastwritetime is today. It will check lastwritetime is greater than yesterday’s date.

Get-childitem -Path D:\PowerShell\ -recurse | where-object {$_.lastwritetime -gt (get-date).addDays(-1) -and -not $_.PSIsContainer}

In the above PowerShell script, the Get-ChildItem cmdlet search for the files within the path specified recursively and check if the lastwritetime is today.

The output of the above script to find the file lastwritetime is today’s date.

Get-ChildItem lastwritetime today
Get-ChildItem lastwritetime today

PowerShell Tip: How to get file creation date in PowerShell!

Conclusion

I hope the above article on how to use the Get-ChildItem lastwritetime attribute to find files by last modified date time and sort them in ascending or descending order.

Cool Tip: How to get the last modified file in the directory using PowerShell!

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