# ArchiveDesktop.ps1
Param( [ValidateSet("Move","Zip")] [string]$Mode = "Move", [int]$DaysInactive = 30, [string]$ArchiveRoot = "$env:USERPROFILE\Documents\DesktopArchive", [switch]$WhatIf) $DesktopPath = [Environment]::GetFolderPath("Desktop")
$Cutoff = (Get-Date).AddDays(-$DaysInactive) # Ensure archive root exists
if (-not (Test-Path $ArchiveRoot) { New-Item -ItemType Directory -Path $ArchiveRoot | Out-Null } # Monthly archive subfolder (yyyy-MM)
$MonthlyFolder = Join-Path $ArchiveRoot (Get-Date -Format "yyyy-MM")
if (-not (Test-Path $MonthlyFolder) { New-Item -ItemType Directory -Path $MonthlyFolder | Out-Null } # Gather eligible files (top-level Desktop only by default; adjust if you want subfolders)
$Files = Get-ChildItem -Path $DesktopPath -File -Force | Where-Object { $_.LastAccessTime -lt $Cutoff } if ($Mode -eq "Move") { foreach ($f in $Files) { $dest = Join-Path $MonthlyFolder $f.Name $counter = 1 # Avoid overwriting: append a suffix if a file with the same name exists while (Test-Path $dest) { $base = [IO.Path]::GetFileNameWithoutExtension($f.Name) $ext = $f.Extension $dest = Join-Path $MonthlyFolder ("{0}_{1}{2}" -f $base, $counter, $ext) $counter++ } if ($WhatIf) { Write-Output "Would move '$($f.FullName)' to '$dest'" } else { Move-Item -LiteralPath $f.FullName -Destination $dest -Force } }
} else { if ($Files.Count -gt 0) { $Paths = $Files | ForEach-Object { $_.FullName } $ZipPath = Join-Path $MonthlyFolder ("DesktopArchive-{0}.zip" -f (Get-Date -Format "yyyyMMdd-HHmm") if ($WhatIf) { Write-Output "Would zip to '$ZipPath' from: $Paths" } else { Compress-Archive -Path $Paths -DestinationPath $ZipPath -Force } } else { Write-Output "No files found eligible for archiving." }
}