Bird
0
0

You want to automate deleting log files older than 14 days but keep the latest 5 files regardless of age. Which approach works best?

hard📝 Application Q8 of 15
PowerShell - Automation Patterns
You want to automate deleting log files older than 14 days but keep the latest 5 files regardless of age. Which approach works best?
ASort files by LastWriteTime descending, skip first 5, then delete files older than 14 days
BDelete all files older than 14 days without sorting
CKeep only files created in last 14 days, delete others
DDelete files older than 14 days and then copy latest 5 files back
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement to keep latest 5 files

    We must sort files by LastWriteTime descending to identify newest files.
  2. Step 2: Skip first 5 newest files and delete older ones beyond 14 days

    Skipping first 5 keeps them safe, then delete files older than 14 days.
  3. Final Answer:

    Sort files by LastWriteTime descending, skip first 5, then delete files older than 14 days -> Option A
  4. Quick Check:

    Keep latest 5 + delete old = Sort, skip, delete [OK]
Quick Trick: Sort descending, skip newest files, then delete old ones [OK]
Common Mistakes:
  • Deleting without sorting
  • Deleting latest files accidentally
  • Copying files unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes