Skip to content

Instantly share code, notes, and snippets.

@Codehunter-py
Created November 16, 2024 21:20
Show Gist options
  • Select an option

  • Save Codehunter-py/3c51992263f098dc6dc44a4ec5cefc9e to your computer and use it in GitHub Desktop.

Select an option

Save Codehunter-py/3c51992263f098dc6dc44a4ec5cefc9e to your computer and use it in GitHub Desktop.
This script deletes files older than 3 months in the specified folder on the F: drive.
# Script Name: Cleanup-OldBackups.ps1
# Description: This script deletes files older than 3 months in the specified folder on the F: drive.
# Define the path to the backup folder
$backupFolderPath = "F:\Data" # Double check path
# Define the age in days (3 months = 90 days)
$ageInDays = 90
# Get the current date
$currentDate = Get-Date
# Find and delete files older than 90 days
Get-ChildItem -Path $backupFolderPath -File -Recurse | ForEach-Object {
# Calculate file age in days
$fileAge = ($currentDate - $_.LastWriteTime).Days
if ($fileAge -ge $ageInDays) {
Write-Output "Deleting file: $($_.FullName)"
Remove-Item -Path $_.FullName -Force
}
}
Write-Output "Cleanup completed. Files older than $ageInDays days have been deleted."
# Set-ExecutionPolicy RemoteSigned
# Check current execution policy
# Get-ExecutionPolicy
# Set it back to Restricted
# Set-ExecutionPolicy Restricted
# Confirm the change
# Get-ExecutionPolicy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment