How to automatically compress old Files on your Windows PC to save Disk Space

Configurare noua (How To)

Situatie

You’ll set up a Windows Task Scheduler job that runs a PowerShell script. The script finds files not accessed in, say, 90 days, compresses them into a ZIP archive, and moves them to an “Archive” folder. You retain the files but reduce live disk usage.

Solutie

Prerequisites

  • Windows 10 or Windows 11 with admin rights.

  • PowerShell (built in).

  • Enough disk space to initially create the ZIP archive.

Files & variables (example)

$SourceFolder = "C:\Users\Public\LargeFiles"
$ArchiveFolder = "D:\Archive\OldFiles"
$DaysThreshold = 90
$DateCutoff = (Get-Date).AddDays(-$DaysThreshold)
$ZipFileName = "Archive_$((Get-Date).ToString('yyyyMMdd')).zip"
$TempZipPath = "C:\Temp\$ZipFileName"

Steps

  1. Create Archive Folder

    • Create D:\Archive\OldFiles (or your target).

    • Ensure you have write permissions.

  2. Save the PowerShell script (e.g., C:\Scripts\CompressOldFiles.ps1):

    param(
    [string]$SourceFolder = "C:\Users\Public\LargeFiles",
    [string]$ArchiveFolder = "D:\Archive\OldFiles",
    [int]$DaysThreshold = 90
    )
    $DateCutoff = (Get-Date).AddDays(-$DaysThreshold)
    $Files = Get-ChildItem -Path $SourceFolder -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastAccessTime -lt $DateCutoff }
    if ($Files.Count -eq 0) { exit }
    $ZipFileName = "Archive_$((Get-Date).ToString('yyyyMMdd')).zip"
    $TempZipPath = Join-Path $env:TEMP $ZipFileName
    Compress-Archive -Path $Files.FullName -DestinationPath $TempZipPath
    Move-Item -Path $TempZipPath -Destination (Join-Path $ArchiveFolder $ZipFileName)
    # Optionally delete originals:
    # foreach ($f in $Files) { Remove-Item $f.FullName }
  3. Open Task Scheduler

    • Create a new task → Give it a name like “Compress Old Files”.

    • Trigger: daily (choose a time when PC is on).

    • Action: Start a program → powershell.exe

      • Add arguments: -File "C:\Scripts\CompressOldFiles.ps1" -SourceFolder "C:\Users\Public\LargeFiles" -ArchiveFolder "D:\Archive\OldFiles" -DaysThreshold 90

    • Conditions: maybe only if idle or if on AC power.

  4. Test it

    • Manually run the task and check you get a ZIP file in the Archive folder.

    • Verify disk space is freed.

    • Adjust DaysThreshold if you want older/newer files.

Troubleshooting & notes

  • If you get permission errors, check that your user account or the scheduled task’s user has read access to the source and write to archive.

  • Large archives can take time — monitor task duration.

  • Consider how long you want to keep archives. You might add a cleanup step (e.g., delete archives older than X days).

  • This doesn’t replace a proper backup: archived files are still on disk (or same machine) so risk remains if drive fails.

Tip solutie

Permanent

Voteaza

(9 din 14 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?