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!

1 reply »

Leave a 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 )

Google photo

You are commenting using your Google 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.