Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Directory lists folders, not files.
Using -Recurse lists files in subfolders, which is not needed here.
✗ Incorrect
The -File parameter lists only files, which is needed to clean up log files.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
Files with LastWriteTime less than 30 days ago are older than 30 days and should be deleted.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -Confirm causes a prompt, which is not desired.
Using -Recurse is unnecessary for files only.
✗ Incorrect
Use -lt to select files older than 7 days and -Force to delete without confirmation.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Name logs only file names without paths.
Using -gt selects newer files, which is incorrect.
✗ Incorrect
Use -lt to select files older than 10 days and FullName to log full file paths.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use -lt to select old files, FullName for full paths to archive, and a date string for the archive name.