Automating and Enhancing the Script
The compress1.ps1 script is designed to create a .7z archive while excluding unnecessary large files and folders,
such as node_modules, dist, .git, node_modules.7z, and .angular.
This significantly reduces the archive size, making it more efficient for storage and sharing.
If the exclusion list (exclude.txt) does not exist, the script automatically creates it.
To make compress1.ps1 more accessible, adding C:\scripts\ to the system PATH allows you to run it from any directory by simply typing compress1.ps1. Additionally, enabling PowerShell script execution ensures it runs without permission issues. This script can be further improved by adding a timestamp to the archive name or allowing custom exclusions, making it a flexible and reusable tool for efficient backups.
How It Works
The script sets the archive name (backup.7z) and checks if the exclude.txt file is present. If missing, it generates one with predefined exclusions. It then runs the 7-Zip command:
powershell
7z a $archiveName -x@$excludeFile .
This command creates a compressed archive while skipping the listed files and directories, ensuring only necessary content is stored.
Running the Script from Any Directory
To execute compress1.ps1 from any location, save it in C:\scripts\ and add this folder to the Windows system PATH. Alternatively, you can run it directly using:
powershell
C:\scripts\compress1.ps1
If execution is blocked, enable script execution with:
powershell
Set-ExecutionPolicy Bypass -Scope CurrentUser -Force
This setup allows for quick and efficient backups without manually specifying exclusions every time.
$archiveName = "backup.7z"
$excludeFile = "$PSScriptRoot\exclude1.txt"
Create an exclusion list if it doesn't exist
if (-Not (Test-Path $excludeFile)) {
Set-Content $excludeFile "node_modulesndist
n.gitnnode_modules.7z
n.angularnpublic
nassets`n*\assets"
}
Run 7-Zip command
7z a $archiveName -x@"$excludeFile" .