How to Create ZIP Archives with Compress-Archive in PowerShell

Log and configuration files being packaged into a compressed ZIP archive for transfer

What You’ll Learn

In this lesson, you will learn how to use PowerShell’s Compress-Archive cmdlet to create ZIP archives. You will package support logs and configuration files into a single file that can be transferred or stored as a backup.

  • Understand what a ZIP archive is and why it is useful.
  • Create an archive from several individual files.
  • Compress an entire folder.
  • Use -DestinationPath and -Force correctly.
  • Avoid common mistakes when packaging logs and configuration files.
Ad

The Concept

A ZIP archive is a single file that can contain multiple files and folders. ZIP files are convenient for sending a collection of files because they are easier to transfer and keep related files together.

PowerShell provides the Compress-Archive cmdlet for creating ZIP files. Its basic form is:

Compress-Archive -Path "files-to-compress" -DestinationPath "archive-name.zip"

The -Path parameter identifies the files or folders to include. The -DestinationPath parameter specifies the ZIP file that PowerShell should create.

You might use this command when:

  • Sending application logs to a support team.
  • Backing up configuration files before making changes.
  • Collecting several diagnostic files into one transfer package.
  • Storing a group of related files for later use.

Before packaging files, you may want to identify the relevant log entries or configuration files. For example, you can search logs and configuration files with Select-String before choosing what to include.

Basic Example

The following example creates a small support folder, adds a log and a configuration file, and then packages both files into support-bundle.zip. Creating sample files makes the example self-contained; in a real situation, these files would already exist.

$supportFolder = Join-Path $HOME "SupportBundle"
$archivePath = Join-Path $HOME "support-bundle.zip"

New-Item -Path $supportFolder -ItemType Directory -Force | Out-Null

"2026-08-18 09:15:22 ERROR Database connection timed out" |
    Set-Content -Path (Join-Path $supportFolder "application.log")

"Environment=Production`nLogLevel=Warning" |
    Set-Content -Path (Join-Path $supportFolder "application.conf")

$filesToArchive = @(
    Join-Path $supportFolder "application.log"
    Join-Path $supportFolder "application.conf"
)

Compress-Archive -Path $filesToArchive -DestinationPath $archivePath -Force

Write-Output "Created archive: $archivePath"

Expected Output

The exact path depends on the user account running PowerShell. For example, the command may display:

Created archive: C:\Users\Alex\support-bundle.zip

The resulting ZIP file contains application.log and application.conf. You can open the ZIP file in File Explorer or use PowerShell to inspect it.

How the Code Works

A process flow showing how PowerShell packages logs or configuration files: select individual files or a folder, choose a separate ZIP destination, optionally decide whether to overwrite an existing archive, create the ZIP with Compress-Archive, and transfer or store the resulting archive.
Select files or a folder, provide a separate destination path, and use Compress-Archive to create a ZIP archive; use -Force only when replacing an existing archive is intentional.
  • $supportFolder stores the folder where the example files are located.
  • $archivePath stores the location and name of the ZIP file to create.
  • Join-Path combines path parts using the correct path separator for the operating system.
  • New-Item -ItemType Directory creates the sample folder. The -Force parameter prevents an error if the folder already exists.
  • Set-Content writes sample log and configuration text to files.
  • The $filesToArchive array holds the two file paths that should be compressed.
  • Compress-Archive creates the ZIP file from the paths in that array.
  • -Force allows PowerShell to replace an existing archive with the same name.

The at-sign syntax, @(...), creates an array. This is useful when you want to pass several individual files to -Path:

$filesToArchive = @(
    "C:\Support\application.log"
    "C:\Support\application.conf"
    "C:\Support\readme.txt"
)

Compress-Archive -Path $filesToArchive -DestinationPath "C:\Support\bundle.zip"

For configuration files that contain XML, consider inspecting the settings before transferring them. You can extract configuration values with Select-Xml when you need to check specific XML values.

Another Example

Instead of listing individual files, you can compress an entire configuration folder. This is useful when an application has several related configuration files, such as JSON, XML, and text files.

$configurationFolder = "C:\Apps\InventoryService\config"
$backupFolder = "C:\Backups"
$archivePath = Join-Path $backupFolder "inventory-config-backup.zip"

New-Item -Path $backupFolder -ItemType Directory -Force | Out-Null

Compress-Archive -Path $configurationFolder -DestinationPath $archivePath -Force

Write-Output "Configuration backup created: $archivePath"

Here, -Path receives a folder instead of an array of individual files. PowerShell includes the folder and its contents in the ZIP archive. The backup folder is created first so that the destination path is available.

Common Mistakes

Forgetting the destination path

Compress-Archive needs both the source path and the name of the ZIP file. Without -DestinationPath, PowerShell does not know where to save the archive.

Accidentally overwriting an archive

If the destination ZIP already exists, the command can fail unless you use -Force. Use -Force when replacing an old archive is intentional. Otherwise, choose a new name or check whether the file already exists first.

Putting the archive inside the folder being compressed

Avoid creating the destination ZIP inside the source folder. For example, when compressing C:\Logs, save the archive somewhere like C:\Backups\logs.zip. Keeping the destination separate makes the package easier to manage and avoids confusing source and destination files.

Including secrets without checking them

Configuration files may contain passwords, access tokens, connection strings, or other sensitive information. Review the files before sending an archive to someone else. A ZIP file is not automatically a secure or encrypted transfer format.

Expecting every file to be included

The Compress-Archive cmdlet does not include hidden files and hidden folders. If an important file is hidden, verify the archive contents after creating it and use a different packaging approach if necessary.

Try It Yourself

Create a folder named C:\Support\PrinterIssue and place these files inside it:

  • printer.log
  • system-info.txt
  • printer-settings.conf

Then create C:\Support\printer-issue.zip containing all three files. Use an array of file paths rather than compressing the entire folder.

Challenge

A support folder is stored in C:\Support\Today. Create an archive named C:\Support\Today-support.zip that contains:

  • Every .log file directly inside C:\Support\Today.
  • The file settings.json from the same folder.

Use Get-ChildItem to find the log files, add the JSON file to the collection, and then use Compress-Archive. Replace the sample paths with real files on your computer if needed.

Solution

$supportFolder = "C:\Support\Today"
$archivePath = "C:\Support\Today-support.zip"

$logFiles = Get-ChildItem -Path $supportFolder -Filter "*.log" -File
$settingsFile = Join-Path $supportFolder "settings.json"

$filesToArchive = @(
    $logFiles.FullName
    $settingsFile
)

Compress-Archive -Path $filesToArchive -DestinationPath $archivePath -Force

Write-Output "Created support archive: $archivePath"

Get-ChildItem finds the log files, and .FullName produces their complete paths. The JSON path is then added to the same array. Finally, Compress-Archive packages the collected files into one ZIP file.

Key Takeaways

  • Compress-Archive creates ZIP files from files, arrays of files, or folders.
  • Use -Path for the items to include and -DestinationPath for the ZIP file location.
  • Use -Force when intentionally replacing an existing archive.
  • Review logs and configuration files before transferring them, especially for secrets.
  • Keep the destination archive outside the folder being compressed whenever possible.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad