Compress Archive - PowerShell


Overview

Compress-Archive is a PowerShell command that creates ZIP archives. It provides a convenient and efficient way to compress files and folders, reducing their size for storage or transfer. This command is particularly useful when dealing with large datasets or when sharing files over networks or email.

Syntax

Compress-Archive -Path <PathToFolder> -DestinationPath <ZippedFilePath> [-CompressionLevel <0-9>] [-Force]

Options/Flags

  • Path (Required): Specifies the path to the folder or files to compress.
  • DestinationPath (Required): Specifies the destination path and filename for the compressed archive.
  • CompressionLevel (Optional): Sets the compression level from 0 (no compression) to 9 (maximum compression). Default is 5.
  • Force (Optional): Overwrites an existing archive file at the DestinationPath without prompting.

Examples

Simple Use:

Compress-Archive -Path "C:\Docs" -DestinationPath "C:\MyDocs.zip"

Setting Compression Level:

Compress-Archive -Path "C:\Photos" -DestinationPath "C:\Photos.zip" -CompressionLevel 8

Force Overwrite:

Compress-Archive -Path "C:\Projects" -DestinationPath "C:\Projects.zip" -Force

Common Issues

  • Insufficient Permissions: Ensure you have sufficient permissions to create files in the destination path.
  • Non-Existing Destination Path: Verify that the specified DestinationPath is a valid directory.
  • Unsupported File Formats: Compress-Archive supports regular files, but not special files like shortcuts or hard links.

Integration

Chaining Commands:

Get-ChildItem -Path "C:\Dir1" | Compress-Archive -DestinationPath "C:\Dir1.zip"

Using with Variables:

$files = Get-ChildItem -Path "C:\Dir2"
Compress-Archive -Path $files -DestinationPath "C:\Dir2.zip"