Overview
This lecture explains the usage, syntax, and options for the PowerShell Compress-Archive cmdlet, which creates compressed (zipped) files from specified files and directories.
Cmdlet Purpose & Function
Compress-Archive creates a .zip archive from one or more files or directories.
- Combines multiple files into a single zipped file for easier storage and distribution.
- Uses the .NET
System.IO.Compression.ZipArchive API with a 2GB maximum file size limit.
Syntax & Usage Patterns
- Accepts file/directory paths via
-Path or -LiteralPath parameters.
- Supports creating, updating, and overwriting archives using
-Update or -Force.
- Can specify archive location with
-DestinationPath.
- Wildcards are accepted with
-Path but not -LiteralPath.
- Offers compression levels:
Optimal (default), Fastest, or NoCompression.
Key Parameters
-Path allows specifying files/folders, supports wildcards, and controls inclusion/exclusion of root directories.
-LiteralPath uses exact file names, no wildcards or escaping.
-DestinationPath sets the archive file's full path (automatically appends .zip if missing).
-CompressionLevel determines the tradeoff between speed and file size.
-Update updates an existing archive with newer files.
-Force overwrites an existing archive file.
-PassThru outputs a FileInfo object of the created archive.
-WhatIf and -Confirm provide safety before executing.
Usage Examples
- Compress multiple files or patterns:
Compress-Archive -Path C:\Folder\*.txt -DestinationPath C:\out.zip
- Compress files without directory structure: Supply only file names in
-Path.
- Include or exclude root directory in archives with or without wildcards.
- Pipe files or directories to
Compress-Archive for flexible scripting.
- Update existing archives with new or updated files using
-Update.
Important Behavior & Notes
- Ignores hidden files and folders by default; use .NET API to include them.
- Recursively piping files can unintentionally duplicate files in the archive.
- The cmdlet uses UTF-8 encoding for file names in archives.
- By default, there is no output unless
-PassThru is used.
Key Terms & Definitions
- Archive — A compressed file containing multiple files/directories (e.g., .zip).
- Cmdlet — A PowerShell command (pronounced "command-let").
- Wildcard — Special character(s) to match multiple files (
* or ?).
- FileInfo/DirectoryInfo — .NET objects representing files and directories.
- CompressionLevel — Sets how much data is compressed.
- Pipeline — Sequence where output of one command is input to another.*
Action Items / Next Steps
- Practice creating, updating, and overwriting archives using various parameter combinations.
- Review how wildcards affect which files/directories are included.
- Try piping files and directories to
Compress-Archive to understand pipeline behavior.