Bird
0
0

Identify the error in this PowerShell script intended to delete log files older than 15 days:

medium📝 Debug Q14 of 15
PowerShell - Automation Patterns
Identify the error in this PowerShell script intended to delete log files older than 15 days:
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-15) } | Remove-Item
AThe filter uses -gt instead of -lt, so it deletes newer files
BRemove-Item cannot be used in a pipeline
CGet-ChildItem does not support -Filter parameter
DThe script is missing the -Recurse flag
Step-by-Step Solution
Solution:
  1. Step 1: Check the date comparison operator

    -gt means greater than, so it selects files newer than 15 days, opposite of intended.
  2. Step 2: Confirm correct operator for old files

    To delete files older than 15 days, use -lt (less than) with AddDays(-15).
  3. Final Answer:

    The filter uses -gt instead of -lt, so it deletes newer files -> Option A
  4. Quick Check:

    Older files need -lt, not -gt [OK]
Quick Trick: Use -lt for files older than a date, not -gt [OK]
Common Mistakes:
  • Using -gt instead of -lt for filtering old files
  • Thinking Remove-Item can't be piped
  • Assuming -Filter is unsupported by Get-ChildItem

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes