In this post, we will share a PowerShell script that automates the archiving of older files in a specified directory. This script helps you keep your workspace organized by moving files that are no longer actively needed to an archive folder.
powershell
$sourceDirectory = 'C:\FilesToArchive'
$archiveDirectory = 'D:\ArchivedFiles'
$daysThreshold = 30
$thresholdDate = (Get-Date).AddDays(-$daysThreshold)
if (-Not (Test-Path -Path $archiveDirectory)) {
New-Item -ItemType Directory -Path $archiveDirectory
Write-Host "Created archive directory: $archiveDirectory"
}
Get-ChildItem -Path $sourceDirectory -File | Where-Object { $_.LastWriteTime -lt $thresholdDate } | ForEach-Object {
Move-Item -Path $_.FullName -Destination $archiveDirectory
Write-Host "Archived: $($_.Name) to $archiveDirectory"
}
$archivedFilesCount = (Get-ChildItem -Path $archiveDirectory -File).Count
Write-Host "Archiving process completed. $archivedFilesCount files were moved to '$archiveDirectory'."Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
Ready when you are.
Try ServerEngine free for 7 days.