Challenge - 5 Problems
Log Cleanup Automation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell log cleanup script?
This script deletes log files older than 7 days in the folder C:\Logs. What output will it produce if there are 3 files older than 7 days named log1.txt, log2.txt, and log3.txt?
PowerShell
Get-ChildItem -Path 'C:\Logs' -Filter '*.txt' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Verbose
Attempts:
2 left
💡 Hint
The -Verbose flag shows which files are being deleted.
✗ Incorrect
The script filters files older than 7 days and deletes them with verbose output. Since 3 files match, all 3 are removed with verbose messages.
📝 Syntax
intermediate2:00remaining
Which option correctly filters log files older than 30 days?
Choose the PowerShell command that correctly lists all .log files in D:\Logs older than 30 days.
Attempts:
2 left
💡 Hint
We want files older than 30 days, so compare LastWriteTime with a date 30 days ago.
✗ Incorrect
Option C correctly filters files with LastWriteTime less than the date 30 days ago, meaning older than 30 days.
🔧 Debug
advanced2:00remaining
Why does this log cleanup script fail with an error?
The script below is intended to delete .log files older than 10 days but throws an error. What is the cause?
Get-ChildItem -Path 'E:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt Get-Date.AddDays(-10) } | Remove-Item
PowerShell
Get-ChildItem -Path 'E:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt Get-Date.AddDays(-10) } | Remove-Item
Attempts:
2 left
💡 Hint
Check how Get-Date and its methods are called in PowerShell.
✗ Incorrect
Get-Date is a command that returns a DateTime object. To call AddDays, you must wrap Get-Date in parentheses.
🚀 Application
advanced2:00remaining
How to schedule a daily log cleanup task using PowerShell script?
You want to automate deleting .log files older than 14 days from C:\AppLogs every day at 2 AM. Which PowerShell command creates a scheduled task to run the cleanup script daily at 2 AM?
Attempts:
2 left
💡 Hint
Use New-ScheduledTaskAction and New-ScheduledTaskTrigger to define the task, then Register-ScheduledTask to create it.
✗ Incorrect
Option A correctly uses PowerShell cmdlets to create an action and trigger, then registers the scheduled task.
🧠 Conceptual
expert3:00remaining
What is the best way to ensure a PowerShell log cleanup script handles locked files gracefully?
You have a script that deletes old log files but sometimes fails because some files are locked by other processes. Which approach best handles this situation?
Attempts:
2 left
💡 Hint
Think about error handling to keep the script running even if some files cannot be deleted.
✗ Incorrect
Try-Catch lets the script continue running and handle locked files by logging or skipping them, which is safer and more reliable.