Bird
0
0

Which of the following is the correct syntax to get files older than 7 days in PowerShell?

easy📝 Syntax Q3 of 15
PowerShell - Automation Patterns
Which of the following is the correct syntax to get files older than 7 days in PowerShell?
AGet-ChildItem | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(7) }
BGet-ChildItem | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }
CGet-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) }
DGet-ChildItem | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(7) }
Step-by-Step Solution
Solution:
  1. Step 1: Understand date comparison for older files

    Files older than 7 days have LastWriteTime less than current date minus 7 days.
  2. Step 2: Match the correct syntax

    Get-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } correctly filters files with LastWriteTime less than (Get-Date).AddDays(-7).
  3. Final Answer:

    Get-ChildItem | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } -> Option C
  4. Quick Check:

    Older files filter = LastWriteTime -lt AddDays(-7) [OK]
Quick Trick: Use -lt with AddDays(-n) to find files older than n days [OK]
Common Mistakes:
  • Using -gt instead of -lt
  • Comparing CreationTime incorrectly
  • Adding days instead of subtracting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes