← Back to all articles

PowerShell · Automation

Automate Your File Archiving

Learn how to configure a PowerShell file-archiving script, run it manually, and schedule it to run every day with Windows Task Scheduler—with important safety checks before deleting source files.

8 min read•Workless Worthmore•Beginner friendly
▣
Work smarter. Build better.

Practical automation guides for everyday work.

What this script does

This PowerShell script creates a ZIP archive of the files inside a source folder, gives the archive a timestamped filename, and then deletes the original contents if the ZIP file exists. You can run it manually whenever you need an archive or configure Windows Task Scheduler to run it on a schedule.

Important safety note: The supplied script deletes source contents after checking only whether the ZIP file exists. A ZIP file can exist even if it is incomplete or corrupt. Start with test files, inspect the archive, and use the safer verification approach described below before enabling deletion on important data.

Step 1: Choose the source and backup folders

The SOURCE_DIR is the folder whose contents will be archived. The BACKUP_DIR is where the ZIP file will be saved. Use full paths and make sure the backup location is not inside the source folder.

$SOURCE_DIR = 'C:\\Users\\YourName\\Documents\\ToArchive'
$BACKUP_DIR = 'D:\\Backups\\WorkFiles'

Replace these example paths with your actual folders. Keep the single quotes around the paths; spaces in Windows folder names are supported when paths are quoted.

Step 2: Understand the timestamped ZIP filename

$TIMESTAMP = Get-Date -Format 'yyyyMMdd_HHmm'
$ZIP_FILE = "$BACKUP_DIR\\Archive_$TIMESTAMP.zip"

Get-Date reads the current date and time. The format produces names such as Archive_20261009_1315.zip. This helps keep each run in a separate archive rather than reusing one filename. The format is precise to the minute, so avoid launching two runs in the same minute with the same backup directory.

Step 3: Create the backup folder if needed

if (-not (Test-Path $BACKUP_DIR)) {
    New-Item -ItemType Directory -Path $BACKUP_DIR -Force | Out-Null
}

Test-Path checks whether the destination exists. If it does not, New-Item creates it. Out-Null hides the normal command output so the script stays tidy.

Step 4: Compress the source contents

Compress-Archive -Path "$SOURCE_DIR\\*" -DestinationPath $ZIP_FILE -Force

Compress-Archive is a built-in PowerShell cmdlet for creating ZIP archives. The wildcard * means the contents of the source directory are included. If you need hidden files, special file types, or large data sets, test the behavior with your specific folders before deploying.

Step 5: Verify before deleting anything

Your original script uses Test-Path $ZIP_FILE and then removes the source contents. That is a basic existence check, not a full integrity check. A safer first deployment is to create the archive and leave the source files in place until you have inspected the ZIP and tested extracting it.

For a safer baseline, replace the deletion block with a message:

if (Test-Path $ZIP_FILE) {
    Write-Host "Archive created: $ZIP_FILE"
    Write-Host "Check the ZIP contents before removing source files."
} else {
    throw "Zip creation failed. Source files preserved."
}

Once you have a tested verification strategy and a separate backup, you can decide whether automated cleanup is appropriate. Do not enable deletion for business-critical files until you have tested restore and recovery.

Full script: Put it all together

Here is the complete script using the safer default: it creates the timestamped ZIP archive and confirms that the archive file exists, but it does not delete the original files. Test it with sample files first, then inspect the ZIP and verify that you can restore its contents.

$SOURCE_DIR = 'C:\\Users\\YourName\\Documents\\ToArchive'
$BACKUP_DIR = 'D:\\Backups\\WorkFiles'
$TIMESTAMP  = Get-Date -Format 'yyyyMMdd_HHmm'
$ZIP_FILE   = "$BACKUP_DIR\\Archive_$TIMESTAMP.zip"

# Create the backup folder if it doesn't exist
if (-not (Test-Path $BACKUP_DIR)) {
    New-Item -ItemType Directory -Path $BACKUP_DIR -Force | Out-Null
}

# Compress the source files into a ZIP archive
Compress-Archive -Path "$SOURCE_DIR\\*" -DestinationPath $ZIP_FILE -Force

# Confirm that the archive exists; keep the original files in place
if (Test-Path $ZIP_FILE) {
    Write-Host "Archive created: $ZIP_FILE"
    Write-Host "Inspect the ZIP and test restoring files before cleanup."
} else {
    throw "ZIP creation failed. Source files preserved."
}

Update $SOURCE_DIR and $BACKUP_DIR to match your folders. Keep the backup destination outside the source folder. Once you have a tested restore process and a separate backup, decide whether any cleanup step is necessary.

Step 6: Run it on demand

  1. Open Notepad or Visual Studio Code.
  2. Paste the script and update the source and backup paths.
  3. Save it as Archive-Files.ps1. In Notepad, choose “All files” so it does not become Archive-Files.ps1.txt.
  4. Open PowerShell and navigate to the folder where you saved the script, for example: cd "C:\\Scripts".
  5. Run it with .\Archive-Files.ps1.
  6. Inspect the output ZIP and confirm that it contains the expected files.

If your organization's execution policy blocks scripts, follow your IT administrator's policy rather than changing machine-wide security settings. You can also launch the script from a PowerShell session configured according to your organization's rules.

Step 7: Schedule it to run every day with Task Scheduler

  1. Open the Windows Start menu and search for Task Scheduler.
  2. Select Create Basic Task and give it a name such as Daily File Archive.
  3. Choose Daily, then select a time when the files are not being actively edited.
  4. For the action, choose Start a program.
  5. In Program/script, enter the PowerShell executable, commonly powershell.exe.
  6. In Add arguments, enter -NoProfile -File "C:\\Scripts\\Archive-Files.ps1", updating the path to your script.
  7. Finish the wizard, then find the task in Task Scheduler and use Run to test it.
  8. Open the task's properties to review its run conditions and security settings. If it must run while you are signed out, configure the account and permissions carefully.

The account running the scheduled task needs permission to read the source folder and write to the backup destination. If the destination is a mapped network drive, scheduled tasks may not see the same drive mappings as your interactive session; use an approved UNC path such as \\\\server\\share\\Backups and verify permissions.

Step 8: Deploy it responsibly

Common issues

“The path does not exist”

Check that the source and destination paths are correct. Quote paths that contain spaces and verify them using Test-Path.

The task works manually but not on schedule

The scheduled task may run under a different account, use a different working directory, or lack permissions. Use absolute paths and verify the task's account and history.

The archive is missing or incomplete

Check whether files are locked, whether the account has read permission, and whether PowerShell reported an error. Do not delete originals based only on the ZIP file's existence.

Conclusion

A small PowerShell script can turn a repetitive archiving task into an on-demand or daily workflow. The deployment is not complete until you have tested the script, checked permissions, validated the archive, and documented how to restore files. Start with a safe copy-only version, then add cleanup only if your verified process requires it.

Want help automating a task?

Workless Worthmore shares practical resources and helps turn repetitive work into documented, maintainable workflows.

Discuss a solution →