Bird
Raised Fist0
PowerShellscripting~20 mins

Log cleanup automation in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of automating log cleanup using PowerShell scripts?
easy
A. To delete old log files and free up disk space
B. To create new log files automatically
C. To rename log files for better organization
D. To compress log files for faster access

Solution

  1. Step 1: Understand log cleanup goal

    The goal is to remove old log files that are no longer needed to save disk space.
  2. Step 2: Identify automation benefit

    Automating this process ensures logs are cleaned regularly without manual effort.
  3. Final Answer:

    To delete old log files and free up disk space -> Option A
  4. Quick Check:

    Log cleanup = delete old logs [OK]
Hint: Log cleanup means removing old logs to save space [OK]
Common Mistakes:
  • Confusing cleanup with creating or renaming logs
  • Thinking cleanup compresses files instead of deleting
  • Assuming automation creates logs automatically
2. Which PowerShell command correctly lists log files older than 30 days in the folder C:\Logs?
easy
A. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(30) }
B. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-30) }
C. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }
D. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(30) }

Solution

  1. Step 1: Identify correct date filter

    We want files older than 30 days, so LastWriteTime should be less than (Get-Date).AddDays(-30).
  2. Step 2: Check command syntax

    Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } uses LastWriteTime -lt (less than) 30 days ago, correctly filtering old files.
  3. Final Answer:

    Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } -> Option C
  4. Quick Check:

    Older than 30 days = LastWriteTime -lt (Get-Date).AddDays(-30) [OK]
Hint: Use -lt (less than) with AddDays(-30) for files older than 30 days [OK]
Common Mistakes:
  • Using -gt instead of -lt for older files
  • Filtering by CreationTime instead of LastWriteTime
  • Using AddDays(30) instead of AddDays(-30)
3. What will be the output of this PowerShell script?
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -WhatIf
medium
A. Throws an error because Remove-Item cannot be piped
B. Lists all .log files older than 7 days and deletes them
C. Deletes all .log files regardless of age
D. Shows which .log files older than 7 days would be deleted without deleting

Solution

  1. Step 1: Understand the pipeline

    The script finds .log files older than 7 days using Get-ChildItem and Where-Object.
  2. Step 2: Analyze Remove-Item with -WhatIf

    -WhatIf shows what would happen without deleting files, so no files are removed.
  3. Final Answer:

    Shows which .log files older than 7 days would be deleted without deleting -> Option D
  4. Quick Check:

    -WhatIf shows actions without executing [OK]
Hint: Remove-Item -WhatIf previews deletion without removing files [OK]
Common Mistakes:
  • Thinking files are deleted with -WhatIf
  • Assuming Remove-Item cannot be piped
  • Confusing filter for files newer than 7 days
4. 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
medium
A. The filter uses -gt instead of -lt, so it deletes newer files
B. Remove-Item cannot be used in a pipeline
C. Get-ChildItem does not support -Filter parameter
D. The script is missing the -Recurse flag

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]
Hint: 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
5. You want to automate deleting log files older than 10 days from C:\Logs and log the deleted filenames to deleted_logs.txt. Which script correctly does this?
hard
A. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-10) } | Remove-Item; Out-File -FilePath 'deleted_logs.txt' -InputObject 'Deleted logs' -Append
B. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } | ForEach-Object { Remove-Item $_.FullName; $_.FullName | Out-File -FilePath 'deleted_logs.txt' -Append }
C. Remove-Item -Path 'C:\Logs\*.log' -Recurse -Force; Get-ChildItem 'deleted_logs.txt' | Out-File -FilePath 'deleted_logs.txt' -Append
D. Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Remove-Item; Add-Content -Path 'deleted_logs.txt' -Value 'Logs deleted'

Solution

  1. Step 1: Filter files older than 10 days

    Use Where-Object with LastWriteTime -lt (Get-Date).AddDays(-10) to select old logs.
  2. Step 2: Remove files and log names

    ForEach-Object removes each file and appends its name to deleted_logs.txt using Out-File -Append.
  3. Final Answer:

    Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } | ForEach-Object { Remove-Item $_.FullName; $_.FullName | Out-File -FilePath 'deleted_logs.txt' -Append } -> Option B
  4. Quick Check:

    Filter old files + remove + log names = Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-10) } | ForEach-Object { Remove-Item $_.FullName; $_.FullName | Out-File -FilePath 'deleted_logs.txt' -Append } [OK]
Hint: Use ForEach-Object to remove and log each file name [OK]
Common Mistakes:
  • Filtering with -gt instead of -lt
  • Logging only a fixed string, not filenames
  • Deleting files without logging
  • Using Remove-Item without filtering