0
0
PowerShellscripting~10 mins

Log cleanup automation in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Log cleanup automation
Start script
Set log folder path
Get list of log files
For each file
Check file age > threshold?
NoSkip file
Yes
Delete file
Repeat for next file
End script
The script starts by setting the folder path, then checks each log file's age. If older than the set days, it deletes the file, repeating until all files are processed.
Execution Sample
PowerShell
 $logPath = "C:\Logs"
 $days = 7
 Get-ChildItem $logPath -Filter *.log | ForEach-Object {
   if (((Get-Date) - $_.LastWriteTime).Days -gt $days) {
     Remove-Item $_.FullName
   }
 }
This script deletes .log files older than 7 days in the C:\Logs folder.
Execution Table
StepActionFile ProcessedFile Age (days)Condition (Age > 7)Result
1Get list of .log filesN/AN/AN/AFiles found: app.log, error.log, old.log
2Check app.logapp.log33 > 7? FalseSkip file
3Check error.logerror.log1010 > 7? TrueDelete file
4Check old.logold.log1515 > 7? TrueDelete file
5End of filesN/AN/AN/AScript ends
💡 All files processed; script ends after checking all files.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
$logPath"C:\Logs""C:\Logs""C:\Logs""C:\Logs""C:\Logs""C:\Logs"
$days777777
Current FileN/Aapp.logerror.logold.logN/AN/A
File AgeN/A31015N/AN/A
Files Deleted001222
Key Moments - 3 Insights
Why does the script skip some files instead of deleting them?
Files are skipped if their age in days is not greater than the threshold (7 days). See execution_table rows 2 and 3 where app.log is skipped because 3 > 7 is False.
How does the script determine the file's age?
The script subtracts the file's LastWriteTime from the current date and checks the Days property. This is shown in execution_table column 'File Age (days)'.
What happens if there are no log files in the folder?
The script gets an empty list and ends immediately after step 1 with no files processed, as implied by the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the file age of error.log at step 3?
A10 days
B3 days
C15 days
D7 days
💡 Hint
Check the 'File Age (days)' column in execution_table row 3.
At which step does the script delete the first file?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Result' column in execution_table to see when 'Delete file' first appears.
If the $days variable was changed to 2, what would happen to app.log at step 2?
AIt would be skipped
BIt would be deleted
CScript would error
DIt would be renamed
💡 Hint
Compare the file age (3 days) to the new threshold (2 days) using the condition in execution_table.
Concept Snapshot
Log cleanup automation in PowerShell:
- Use Get-ChildItem to list log files
- Check each file's LastWriteTime against current date
- Delete files older than a set number of days
- Loop through all files
- Script ends after all files checked
Full Transcript
This PowerShell script automates cleaning up log files by deleting those older than a specified number of days. It starts by setting the folder path and the age threshold. Then it lists all .log files in the folder. For each file, it calculates the age by subtracting the file's last write time from the current date. If the file is older than the threshold, it deletes the file; otherwise, it skips it. This process repeats until all files are checked, then the script ends.