Here's a 66% solution (lunch is over). I couldn't get the native zip to work with PowerShell but if you have PowerShell Community Extensions installed, it should be a snap to swap out the zip call
How to create a zip archive with PowerShell?
# purloined from http://blogs.inetium.com/blogs/mhodnick/archive/2006/08/07/295.aspx
# I am not smart enough to make it work. Creates an empty .zip file for me
Function ZipIt
{
param
(
[string]$path
, [string]$files
)
if (-not $path.EndsWith('.zip')) {$path += '.zip'}
if (-not (test-path $path)) {
set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
}
$zipFile = (new-object -com shell.application).NameSpace($path)
#$files | foreach {$zipfile.CopyHere($_.fullname)}
foreach($file in $files)
{
$zipfile.CopyHere($file)
}
}
# this script is reponsible for enumerating subfolders
# for each file it finds, it will test the age on it
# returns an array of all the files meeting criteria
Function Walk-SubFolder
{
param
(
[string]$RootFolder
)
$searchPattern = "*.*"
$maxAge = 90
$now = Get-Date
# receiver for the files
[string[]] $agedOutFileList = @()
foreach($file in [System.IO.Directory]::GetFiles($RootFolder, $searchPattern, [System.IO.SearchOption]::AllDirectories))
{
# test whether the file meets the age criteria
$age = [System.IO.File]::GetLastWriteTime($file)
$days = ($now - $age).Days
if (($now - $age).Days -ge $maxAge)
{
# this needs to be archived
Write-Host("$file is aged $days days")
$agedOutFileList = $agedOutFileList + $file
}
}
return $agedOutFileList
}
Function PurgeFiles
{
param
(
$fileList
)
foreach($file in $fileList)
{
# this should be in a try/catch block, etc, etc
[System.IO.File]::Delete($file)
}
}
$sourceFolder = "C:\tmp\so"
$zipFile = "C:\tmp\so.zip"
$oldFiles = Walk-SubFolder $sourceFolder
ZipIt $zipFile $oldFiles
#This is commented out as the zip process is not working as expected
#PurgeFiles $oldFiles
I'll see if I can work on it later to get the zip working as expected.