0
0
PowerShellscripting~20 mins

Log cleanup automation in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Log Cleanup Automation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
AError: Remove-Item : Access to the path 'C:\Logs\log1.txt' is denied.
B
VERBOSE: Removing item C:\Logs\log1.txt
VERBOSE: Removing item C:\Logs\log2.txt
VERBOSE: Removing item C:\Logs\log3.txt
CNo output
D
VERBOSE: Removing item C:\Logs\log1.txt
VERBOSE: Removing item C:\Logs\log2.txt
Attempts:
2 left
💡 Hint
The -Verbose flag shows which files are being deleted.
📝 Syntax
intermediate
2: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.
AGet-ChildItem -Path 'D:\Logs' -Filter '*.log' | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(30) }
BGet-ChildItem -Path 'D:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-30) }
CGet-ChildItem -Path 'D:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }
DGet-ChildItem -Path 'D:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(30) }
Attempts:
2 left
💡 Hint
We want files older than 30 days, so compare LastWriteTime with a date 30 days ago.
🔧 Debug
advanced
2: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
AGet-Date.AddDays(-10) is incorrect syntax; Get-Date is a command and needs parentheses: (Get-Date).AddDays(-10)
BWhere-Object cannot compare dates using -lt operator
CThe filter '*.log' should be '*.txt' to match log files
DRemove-Item requires -Force to delete files older than 10 days
Attempts:
2 left
💡 Hint
Check how Get-Date and its methods are called in PowerShell.
🚀 Application
advanced
2: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?
A$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-File C:\Scripts\CleanupLogs.ps1'; $trigger = New-ScheduledTaskTrigger -Daily -At 2am; Register-ScheduledTask -TaskName 'DailyLogCleanup' -Action $action -Trigger $trigger
BNew-ScheduledTask -TaskName 'DailyLogCleanup' -ScriptPath 'C:\Scripts\CleanupLogs.ps1' -Daily -At 2am
Cschtasks /create /tn DailyLogCleanup /tr 'PowerShell.exe -File C:\Scripts\CleanupLogs.ps1' /sc hourly /st 02:00
DRegister-ScheduledTask -TaskName 'DailyLogCleanup' -Script 'C:\Scripts\CleanupLogs.ps1' -Schedule Daily -Time 2am
Attempts:
2 left
💡 Hint
Use New-ScheduledTaskAction and New-ScheduledTaskTrigger to define the task, then Register-ScheduledTask to create it.
🧠 Conceptual
expert
3: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?
ARun the script as Administrator to bypass file locks
BAdd -Force parameter to Remove-Item to force deletion of locked files
CUse Start-Sleep to wait 10 seconds before deleting files to avoid locks
DUse Try-Catch blocks around Remove-Item to catch errors and log locked files without stopping the script
Attempts:
2 left
💡 Hint
Think about error handling to keep the script running even if some files cannot be deleted.