简体   繁体   中英

Powershell get list of all folders and files modified in the last 4 months in ascending order

I've tried the following code to get a list of files and folders sorted by last modified date:

Get-ChildItem -Force -Recurse -File -Path "E:\ES" -ErrorAction SilentlyContinue | Where-Object { $_.CreationTime.Date -lt (Get-Date).Date } | Sort CreationTime -Descending | Format-Table -Wrap

Can this code be edited such that the list of folders and files er limited to those modified in the last 4 months?

Thank you in advance! You guys have been so so helpful with answering all my previous questions!

Basically, all you have to change are:

  • the date to compare against ( (Get-Date).Date gets you the todays date at midnight, (Get-Date).AddMonths(-4).Date gets you the date 4 months ago at midnight)
  • the comparison operator: -ge will get you all dates greater or equal to the date of 4 months ago)
  • the property you need: LastWriteTime is thedate the file was last modified. CreationTime can also be the date the file was copied to its final destination and is not necessarily the actual date the file was first created.
$refDate = (Get-Date).AddMonths(-4).Date  # 4 months ago at midnight

Get-ChildItem -Path "E:\ES" -Force -Recurse -File -ErrorAction SilentlyContinue | 
Where-Object { $_.LastWriteTime -ge $refDate } | 
Sort-Object LastWriteTime -Descending | 
Format-Table -Wrap

The above wil list only files. If you want Directories aswell just remove the -File switch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM