Compress-Archive (Microsoft.PowerShell.Archive) - PowerShell
Overview
- Creates compressed archive (zipped file) from specified files/directories.
- Utilizes System.IO.Compression.ZipArchive API.
- Maximum file size: 2GB.
- Ignores hidden files and folders by default.
Cmdlet Syntax
Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>]
[-PassThru]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Key Parameters
- Path: Specifies files to add; supports wildcards.
- DestinationPath: Required; specifies output archive path.
- CompressionLevel: Specifies compression method: Optimal, NoCompression, Fastest.
- PassThru: Outputs a file object.
- Update: Updates existing archive with newer file versions.
- Force: Overwrites existing archive file.
Key Features
- CompressionLevel
- Optimal (default): Balance compression and speed.
- Fastest: Prioritizes speed, may increase size.
- NoCompression: No compression.
- Path vs. LiteralPath: LiteralPath ignores wildcards.
Examples
- Compress specific files:
$compress = @{
Path = "C:\Reference\Draftdoc.docx", "C:\Reference\Images\*.vsd"
CompressionLevel = "Fastest"
DestinationPath = "C:\Archives\Draft.zip"
}
Compress-Archive @compress
- Compress directory including root:
Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft.zip
- Compress directory excluding root:
Compress-Archive -Path C:\Reference\* -DestinationPath C:\Archives\Draft.zip
- Update archive with new/updated files:
Compress-Archive -Path C:\Reference -Update -DestinationPath C:\Archives\Draft.zip
Notes
- Hidden Files: Not included by default.
- UTF-8 Encoding: Used by
Compress-Archive.
- Recursion: Can cause duplicate files in archive if not managed.
Related Links
This cmdlet is an efficient way to package and compress multiple files for easier distribution and storage, with a focus on flexibility and integration within PowerShell workflows.