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
Log cleanup automation
📖 Scenario: You are managing a server that generates many log files daily. To save disk space, you want to automatically delete old log files that are no longer needed.
🎯 Goal: Create a PowerShell script that deletes log files older than a certain number of days from a specific folder.
📋 What You'll Learn
Create a variable with a list of log files in a folder
Create a variable for the age limit in days
Use a loop to find and delete files older than the age limit
Print the names of deleted files
💡 Why This Matters
🌍 Real World
Automating log cleanup helps keep servers running smoothly by freeing disk space and reducing manual work.
💼 Career
System administrators and DevOps engineers often write scripts like this to maintain healthy server environments.
Progress0 / 4 steps
1
Get the list of log files
Create a variable called logFiles that stores all files with the extension .log from the folder C:\Logs using Get-ChildItem.
PowerShell
Hint
Use Get-ChildItem with -Path and -Filter to get log files.
2
Set the age limit for deletion
Create a variable called daysLimit and set it to 30 to represent the number of days after which log files should be deleted.
PowerShell
Hint
Just assign the number 30 to daysLimit.
3
Delete old log files
Use a foreach loop with the variable file to go through logFiles. Inside the loop, check if file.LastWriteTime is older than (Get-Date).AddDays(-daysLimit). If yes, delete the file using Remove-Item.
PowerShell
Hint
Use foreach to loop. Use if to compare dates. Use Remove-Item to delete.
4
Print deleted file names
Modify the loop to print the name of each deleted file using Write-Output right after deleting it. Use $file.Name to get the file name.
PowerShell
Hint
Use Write-Output to print the deleted file name inside the if block.
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
Step 1: Understand log cleanup goal
The goal is to remove old log files that are no longer needed to save disk space.
Step 2: Identify automation benefit
Automating this process ensures logs are cleaned regularly without manual effort.
Final Answer:
To delete old log files and free up disk space -> Option A
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?
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
Step 1: Check the date comparison operator
-gt means greater than, so it selects files newer than 15 days, opposite of intended.
Step 2: Confirm correct operator for old files
To delete files older than 15 days, use -lt (less than) with AddDays(-15).
Final Answer:
The filter uses -gt instead of -lt, so it deletes newer files -> Option A
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?