Applicable products
√ Cortex Project History 1.x
√ Cortex 365
Prerequisites
- Administrative rights on the Cortex host server
Assumptions
- The server or account where this script is run has rights to the files.
Overview
The following script includes a section at the top to edit.
Edit the values in the following lines (as required):
- 22 $backupFolder
Save as a .ps1 (PowerShell script) and execute.
###### script = File-Cleanup-v1.0.ps1 ###### ###### Script Version History ###### Version Date Editor Notes ###### 0.1 03MAR2023 MEH Initial creation ###### 1.0 03MAR2023 MEH v1.0 published ###### Example Scheduled Task Entry: ###### powershell.exe -NoProfile -WindowStyle Hidden -command "& {C:\Scripts\File-Cleanup-v1.0.ps1 Daily 30}" ###### Where Daily, and 30 represent the Schedule, NumDays of backup to retain ###### Parameters to pass into the script ###### param( [string]$Schedule, [string]$NumDays ) ###### Un-comment out the following to test the script ###### # $Schedule="Daily" # $NumDays="30" ###### Define the Backup Folder Path and append the schedule to the end ###### $backupFolder = "C:\folder\" + $Schedule ###### Define the number of Days to retain files ###### $Days = "-" + $NumDays ###### Get the current date ###### $CurrentDate = Get-Date ###### Calculate the number of days to delete ###### $DatetoDelete = $CurrentDate.AddDays($Days) ###### Execute the routine to remove all items based on the DatetoDelete variable. Recure through all files and folders. ###### Get-ChildItem $backupFolder -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item ############################################################################################# ############################################ END ############################################ #############################################################################################
Create a Scheduled Task and pass through the parameters
In the scheduled task, pass through the parameters for schedule and number of days to retain.
Example Schedule Task entry.
powershell.exe -NoProfile -WindowStyle Hidden -command "& {C:\Scripts\File-Cleanup-v1.0.ps1 Daily 30}"
Where "Daily" is a sub-folder and "30" is the number of days of backups to retain.
powershell.exe -NoProfile -WindowStyle Hidden -command "& {C:\Scripts\File-Cleanup-v1.0.ps1 Daily 30}"
Where "C:\Scripts\File-Cleanup-v1.0.ps1" is the path and location of the target script.
If you require assistance, please let us know.
The End.