Navigate to the directory that contains your lab files; here is where all the action happens. If you did not specify a custom -Path when running the scripts, it should be ~/random.
- Open the terminal and, using a code editor (vi, nano or VS Code), create a new file called Clear-LogFiles.ps1. Remember, CamelCase is just a convention.
- Enter the following content. I suggest typing it by yourself rather than copy-pasting it.
$Today = Get-Date $TotalFileSize = 0 $FilesToDelete = Get-ChildItem . -Recurse -File | Where-Object {[math]::Floor(($Today - $_.LastWriteTime).TotalDays) -eq 30} Write-Host "The following files will be deleted:" Write-Host $FilesToDelete.FullName foreach ($File in $FilesToDelete) { $TotalFileSize += $File.Length Remove-Item -Path $File -WhatIf } Write-Host "Total space cleared: $([math]::Round($TotalFileSize/[math]::Pow(1024, 2))) MB"
There is no need to actually delete the files. If you would like to anyway, remove the -WhatIf switch from line 12.
It is recommended to use Visual Studio Code to write this script since its IntelliSense auto-completion is very helpful. You could write this script at the terminal itself, in which case, use shorter versions of parameters and tab-completions as necessary.