How to Use PowerShell Zip and PowerShell Unzip on Archives

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

PowerShell Zip and PowerShell Unzip are two commands in the Windows operating system that allow you to compress files into a ZIP or extract files from a ZIP. These commands are also accessible via scripts commonly known as “PowerShell Packages” which can be found in your Start menu under All Programs > Microsoft Script Center, where they might have been listed by searching for “script.”

The “powershell expand-archive” is a command that allows users to expand archives. If you are having trouble with your zip files, try using the “powershell unzip” instead.

How to Use PowerShell Zip and PowerShell Unzip on Archives

Compressing and expanding single or numerous files is a useful skill that all PowerShell users should learn right away. Perhaps you’d want to reduce the size of your files or store them in a location other than a folder. If that’s the case, use PowerShell to unzip and compress your files using commands.

With this lesson, you’ll learn how to compress and unzip files in PowerShell in a variety of methods.

Ready? Let’s get this party started!

Prerequisites

PowerShell 7.1.x will be used in the examples in this lesson. The commands will run in Windows PowerShell 5.1 as well.

How to Manage Zip Files in Linux is a related topic.

Using the Compress-Archive Command to Compress Files

For years, you’ve been using software to compress and archive data by clicking on a graphical user interface (GUI). However, did you know that you can do the same thing using PowerShell? You can easily compress files with the Compress-Archive command.

Consider the following scenario: you have a few files to give to a client. The files reside in a folder on your computer that looks somewhat like this. For executing commands, this tutorial uses these files, although you may use whatever files you have on hand.

Viewing a Folder's Files List Viewing a Folder’s Files List

One by one, files are compressed.

You’ll need two things to compress a single file: a source and a destination.

To compress a single file (widget.png) into a ZIP file, open PowerShell as administrator and perform the command below (archive.zip). The -Path argument instructs the Compress-Archive command where to find the compressed file. The -DestinationPath specifies where the file should be archived.

-Path widget.png -DestinationPath archive.zip Compress-Archive

Separate several files with a comma to compress them all at once, e.g. -Path widget.png,file1.txt,manual.pdf -DestinationPath archive.zip Compress-Archive -Path widget.png,file1.txt,manual.pdf

All Files in a Folder are Compressed

If you have a folder full of structured files that you want to compress all at once, using wildcard characters like an asterisk (*) to represent all of them is great.

To compress and archive (Compress-Archive) all files (*) from the C:Demo folder to the archive.zip file, use the command below.

Compress-Archive ‘C:Demo*’ -Path archive.zip -DestinationPath

Perhaps you just want to compress files of the same kind now. If this is the case, combine the wildcard symbol with the file extension to compress.

To compress and archive all log files (*.log) to the archive.zip file, use the command below.

Compress-Archive -DestinationPath archive.zip -Path ‘*.log’

Compression Levels Defined

You’ve been compressing files using the default compression level (Optimal) in the previous examples, but let’s go through the other compression levels now. When creating an archive file, compression levels determine how much compression is used.

You may pick from three compression level settings listed below:

  • Compression time is dependent on the number and size of files. Optimal (Default) – Compression time is dependent on the number and size of files.
  • Fastest — This is the most efficient compression technique, but it results in bigger file sizes.
  • NoCompression — Does not compress file sizes.

To observe how each compression setting works, use any of the instructions below. Each command adds log (*.log) files to the archive.zip file and compresses them.

# Optimal (Default compression level) Compress-Archive -DestinationPath archive.zip -Path ‘*.log’ # Fastest compression level Compress-Archive -DestinationPath archive.zip -Path ‘*.log’ -CompressionLevel Fastest # No compression Compress-Archive -DestinationPath archive.zip -Path ‘*.log’ -CompressionLevel NoCompression

Files to be Added to a Compressed File

Don’t worry if you forgot to add a file to an archive. You may use the -Update argument to add files to an existing archive.

To add (-Update) your desired file (manual.pdf) to the current archive, run the command below (archive.zip).

‘manual.pdf’ -Path Compress-Archive -Update archive.zip -DestinationPath

The -Update option instructs the Compress-Archive command to automatically replace an existing file with a new one. However, be cautious while adding files to the archive. You don’t want to accidentally overwrite an existing file.

How to Manage ZIP Files in Python is a related topic.

Using the Expand-Archive Command to Expand Zip Files

Expanding an archive is not all that different from compressing files. You must use the Expand-Archive command with the source and destination paths specified. You may extract archived files with the Expand-Archive command.

To extract the contents of the archive.zip file, use the Expand-Archive command. The command creates a destination folder in the working directory since no target path is supplied. The archive file’s name is the same as the target folder’s name (archive).

-Path archive.zip Expand-Archive C:DemoArchive -DestinationPath

To disable all confirmation prompts, use the -Force argument.

The extracted filenames may vary from the source if you use Expand-Archive with archives that include files that were not compressed in a UTF-8 format.

Using.NET, you may list compressed files.

There’s no way to peep into an archive without first extending it using the previous two cmdlets you’ve learnt about. Maybe you’ve lost track of which files you’ve previously compressed. If that’s the case, extracting everything only to double-check which files you’ve previously archived is a waste of time.

The.NET ZipFile class provides a better method to peer into an archive. The.NET ZipFile class, which is part of the System.IO.Compression namespace, has a function called OpenRead(String) that allows you to look into an existing archive.

Use the OpenRead(“$pwdarchive.zip”) command to read each file in the archive.zip file and give the results to the $archive variable.

# (pwd) – current working directory $archive = [System.IO.Compression.ZipFile]::OpenRead(“$pwdarchive.zip”) $archive = [System.IO.Compression.ZipFile]::OpenRead(“$pwdarchive.zip”) $archive = [System.

Run the command below to get a list of all the files in the $archive variable, organized by name (Select-Object -Property Name)

Select-Object -Property Name $archive.Entries

The command for each compressed file in the archive.zip file is provided below.

Without extracting the compressed files, you may list them by name. Without extracting the compressed files, you may list them by name.

Refer to the Entries property instead if you want to see the attributes of each file.

As illustrated in the figure below, you’ll now see the attributes of each file in the archive.

Without extracting the files, you may list the compressed files and their properties. Without extracting the files, you may list the compressed files and their properties.

The 7Zip4PowerShell Module in Action

A lesson on compressing and extending zip archives would be incomplete without discussing 7Zip4PowerShell, another compression package. 7Zip4PowerShell has several new features that make it a more feature-rich option.

You’ve already studied about.NET classes and methods, for example. The command to list archived files is easier to use with the 7Zip4PowerShell module.

To get a list of all the files in the archive.zip file, use the Get-7Zip command (-ArchiveFileName .archive.zip).

The Compress-7Zip cmdlet in 7Zip4PowerShell provides you additional choices for various sorts of archives, including Tar, GZip, BZip2, SevenZip, XZ, and Zip.

Using the Get-7Zip Command to List Compressed Files Using the Get-7Zip Command to List Compressed Files

Run the Get-7ZipInformation command, like this: Get-7ZipInformation -ArchiveFileName archive.zip, to get information about the archive itself. The command displays all of the archive.zip file’s properties.

Conclusion

You’ve learned how to use PowerShell’s built-in commands and the 7Zip4PowerShell module to unzip and zip files. This newfound understanding comes in helpful when it comes to downsizing your files or just transferring many files across as a single file. Is that, however, all PowerShell has to offer?

Why not create a script to archive data and automatically email or move them to long-term storage instead of performing commands as a primitive backup solution?

PowerShell is a command-line tool that provides an interface for working with Microsoft Windows PowerShell. It includes a built in archive functionality to expand and compress archives. Reference: powershell expand-archive multiple files.

Frequently Asked Questions

How do I unzip a zip file in PowerShell?

A: To unzip a zip file with PowerShell, you can use the UnZip module.

How do I unzip a ZIP Archive file?

A: Unzip is a command that can be found in any convenience store, on the internet, or with directions written by your mom.

How do I open a ZIP Archive folder?

A: On macOS, you can open a ZIP Archive by simply double clicking it. There is no need to do anything else except wait for the archive to unzip itself and decompress all of its content.

Related Tags

  • powershell read zip file without extracting
  • powershell expand-archive single file
  • powershell compress-archive
  • powershell unzip all files in a directory
  • powershell compress-archive delete original