0
0
PowerShellscripting~10 mins

Log cleanup automation in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to list all files in the Logs folder.

PowerShell
Get-ChildItem -Path 'C:\Logs' [1]
Drag options to blanks, or click blank then click option'
A-Recurse
B-Directory
C-File
D-Hidden
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Directory lists folders, not files.
Using -Recurse lists files in subfolders, which is not needed here.
2fill in blank
medium

Complete the code to delete files older than 30 days.

PowerShell
Get-ChildItem -Path 'C:\Logs' -File | Where-Object { $_.LastWriteTime [1] (Get-Date).AddDays(-30) } | Remove-Item
Drag options to blanks, or click blank then click option'
A-gt
B-lt
C-eq
D-ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using -gt deletes files newer than 30 days, which is wrong.
Using -eq or -ne does not filter by age properly.
3fill in blank
hard

Fix the error in the script to avoid deleting files without confirmation.

PowerShell
Get-ChildItem -Path 'C:\Logs' -File | Where-Object { $_.LastWriteTime [1] (Get-Date).AddDays(-7) } | Remove-Item [2]
Drag options to blanks, or click blank then click option'
A-lt
B-Confirm
C-Force
D-Recurse
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Confirm causes a prompt, which is not desired.
Using -Recurse is unnecessary for files only.
4fill in blank
hard

Fill both blanks to create a script that logs deleted file names to a text file.

PowerShell
$deletedFiles = Get-ChildItem -Path 'C:\Logs' -File | Where-Object { $_.LastWriteTime [1] (Get-Date).AddDays(-10) }
$deletedFiles | Remove-Item -Force
$deletedFiles | Select-Object -ExpandProperty [2] | Out-File 'C:\Logs\deleted_files.txt'
Drag options to blanks, or click blank then click option'
A-lt
BName
CFullName
D-gt
Attempts:
3 left
💡 Hint
Common Mistakes
Using Name logs only file names without paths.
Using -gt selects newer files, which is incorrect.
5fill in blank
hard

Fill all three blanks to create a script that archives logs older than 15 days and deletes originals.

PowerShell
$oldLogs = Get-ChildItem -Path 'C:\Logs' -File | Where-Object { $_.LastWriteTime [1] (Get-Date).AddDays(-15) }
Compress-Archive -Path $oldLogs.[2] -DestinationPath 'C:\Archive\logs_[3].zip'
$oldLogs | Remove-Item -Force
Drag options to blanks, or click blank then click option'
A-lt
BFullName
C(Get-Date -Format 'yyyyMMdd')
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using Name instead of FullName causes archive errors.
Using -gt selects newer files, which is wrong.
Not using a date in the archive name causes overwriting.