What if your computer could clean up its own mess without you doing anything?
Why Log cleanup automation in PowerShell? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have hundreds of log files piling up on your computer every day. You try to delete old logs manually by opening folders, sorting files by date, and deleting them one by one.
This manual method is slow and boring. You might miss some files or accidentally delete important ones. It takes a lot of time and effort, especially if you have to do it regularly.
Log cleanup automation uses a simple script to find and delete old log files automatically. It saves time, avoids mistakes, and keeps your system clean without you lifting a finger.
Open folder > Sort by date > Select old files > Delete
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Force
With log cleanup automation, you can keep your system tidy effortlessly and focus on more important tasks.
A system administrator schedules a script to delete log files older than 30 days every night, preventing disk space from filling up and avoiding system slowdowns.
Manual log cleanup is slow and error-prone.
Automation scripts delete old logs quickly and safely.
This frees up time and keeps your system healthy.
Practice
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 AQuick Check:
Log cleanup = delete old logs [OK]
- Confusing cleanup with creating or renaming logs
- Thinking cleanup compresses files instead of deleting
- Assuming automation creates logs automatically
C:\Logs?Solution
Step 1: Identify correct date filter
We want files older than 30 days, so LastWriteTime should be less than (Get-Date).AddDays(-30).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.Final Answer:
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } -> Option CQuick Check:
Older than 30 days = LastWriteTime -lt (Get-Date).AddDays(-30) [OK]
- Using -gt instead of -lt for older files
- Filtering by CreationTime instead of LastWriteTime
- Using AddDays(30) instead of AddDays(-30)
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -WhatIfSolution
Step 1: Understand the pipeline
The script finds .log files older than 7 days using Get-ChildItem and Where-Object.Step 2: Analyze Remove-Item with -WhatIf
-WhatIf shows what would happen without deleting files, so no files are removed.Final Answer:
Shows which .log files older than 7 days would be deleted without deleting -> Option DQuick Check:
-WhatIf shows actions without executing [OK]
- Thinking files are deleted with -WhatIf
- Assuming Remove-Item cannot be piped
- Confusing filter for files newer than 7 days
Get-ChildItem -Path 'C:\Logs' -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-15) } | Remove-ItemSolution
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 AQuick Check:
Older files need -lt, not -gt [OK]
- Using -gt instead of -lt for filtering old files
- Thinking Remove-Item can't be piped
- Assuming -Filter is unsupported by Get-ChildItem
C:\Logs and log the deleted filenames to deleted_logs.txt. Which script correctly does this?Solution
Step 1: Filter files older than 10 days
Use Where-Object with LastWriteTime -lt (Get-Date).AddDays(-10) to select old logs.Step 2: Remove files and log names
ForEach-Object removes each file and appends its name to deleted_logs.txt using Out-File -Append.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 BQuick 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]
- Filtering with -gt instead of -lt
- Logging only a fixed string, not filenames
- Deleting files without logging
- Using Remove-Item without filtering
