PowerShell

PowerShell: Zip multiple Folder or Files at once with Compress-Archive

Compressing files is a common task. For saving time, you can use PowerShell to automate the compression process. In this blog post I will show you how to compress multiple folders at once with the PowerShell Cmdlet Compress-Archive. The compressed zip files will be stored separately in a specific file. Let’s dive in.

Compressing multiple Folders and Files

In my example I want to compress all subfolders in C:\temp.

Running the script below leads to the follwing result.

Here is the code to capture and compress the subfolders and the files they contain. The ZIP files will be saved in the specified location.

Open PowerShell ISE or Visual Studio Code and copy the lines below to a new file.

Note: You need to edit line 6 and 9 according to your requirements. These lines contain the source folder and the target folder for the zip files.

<# This script uses Compress archives (ZIP) to compress multiple (sub)folders 
and saves them individually in separate zip files.
#>

# Specify source folder
$source = 'C:\temp'

# Specify zip file location folder (destination folder, make sure it exists)
$destination = 'C:\ZipFiles'

# Action
$subfolders = Get-ChildItem $source -Directory -Recurse
foreach ($s in $subfolders) {

$folderpath = $s.FullName
$foldername = $s.Name

Compress-Archive `
-Path $folderpath `
-DestinationPath $destination\$foldername

}

Now go for it!

See you next time with PowerShell!

7 replies »

  1. Is there a way to treat the zip file as the folder?

    from:
    root folder
    subfolder1
    content1
    subfolder2
    content2

    to:
    root folder
    zipfile1
    content1
    zipfile2
    content2

    Like

  2. Hi, this script is interesting , Can you help me please, what is the sintax for Compressing Multiples Archives into multiples archives.zip

    Like

  3. Can you show how to compress folders only. the folders can have multiple sub folders or files

    Thank you

    Like

  4. Hi,
    First of all thank you very much for sharing knowledge 🙂

    I would like to delete the folders after compressing, is it possible?

    Like

  5. Hi,
    First of all thank you very much for sharing knowledge 🙂

    I would like to delete the folders after compressing, is it possible?

    Like

Leave a Reply to Pablo Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.