Essentially, the ZIP file format reduces the size of files by compressing them into a single file. This process saves disk space, encrypts data, and makes it easy to share files with others. While there are many approaches to zip and unzip files in Windows; in this post, we will show you how to zip and unzip files using the PowerShell utility on a Windows 11/10 PC.
How to Zip and Unzip files using PowerShell in Windows 11/10
Using PowerShell to zip or unzip files requires some technical knowledge, but it doesn’t necessarily require advanced knowledge. PowerShell commands are quite straightforward, particularly for basic operations such as zipping and unzipping files.
To create Zip files you begin by compressing some files into a ZIP file archive using the Compress-Archive cmdlet. It takes the path to any files you want to compress—multiple files are separated with a comma—and archives them in the destination you specify.
Zip files using PowerShell
Press Windows key + X to open the Power User Menu and select Terminal (Admin) from the list of options. You’ll see a User Account Control prompt. Press Yes to continue opening PowerShell as an administrator.
Next, type in the syntax below, replacing <PathToFiles> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
Compress-Archive -LiteralPath <PathToFiles> -DestinationPath <PathToDestination>
Note: When you provide the destination path, be sure to give the archive file a name or PowerShell will save it as “.zip” where you specify. Also, bear in mind that quotations around the path are only necessary when the file path contains a space.
Alternatively, to zip the entire contents of a folder and all of its subfolders, you can use the same syntax as above, replacing <PathToFiles> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
It should look like as shown in the image below.
This command puts the path to a directory with multiple files and folders in it without specifying individual files. PowerShell takes everything inside of the root directory and compresses it, subfolders, and all.
Read: How to open .TAR.GZ, .TGZ or .GZ. Files.
The wildcard character (*) function
The Compress-Archive cmdlet lets you use a wildcard character (*) to expand the functionality even further. When you use the character, you can exclude the root directory, compress only files in a directory, or choose all files of a specific type. To use a wildcard with Compress-Archive, you must use the -Path parameter instead, as -LiteralPath does not accept them.
Now, from both examples given above, you have seen how to include the root directory and all of its files and subdirectories when creating an archive file. However, if you want to exclude the root folder from the Zip file, you can use a wildcard to omit it from the archive. By adding an asterisk (*) to the end of the file path, PowerShell will only grab what’s inside of the root directory. The correct syntax is presented below.
Compress-Archive -Path C:\path\to\file\* -DestinationPath C:\path\to\archive.zip
Now, in the case whereby you have a folder with a bunch of different file types (.docx, .txt, .jpg, etc.) but only want to compress all of one type, you can use the syntax below. PowerShell will archive the files specified without touching the others explicitly. Bear in mind that subdirectories and the files of the root folder aren’t included in the archive with this method.
Compress-Archive -Path C:\path\to\file\*.docx -DestinationPath C:\path\to\archive.zip
The following image shows how the output looks like in this case:
Lastly, if you want an archive that only compresses files in the root directory and leaves all its subdirectories, you will use the star-dot-star (*.*) wildcard to zip the files with the syntax below. With this method too, the root directory’s subdirectories and the root directory aren’t included in the archive.
Compress-Archive -Path C:\path\to\file\*.* -DestinationPath C:\path\to\archive.zip
Now, it’s imperative to point out that even after the archive is complete, you can update an existing zipped file with the use of the -Update parameter with the correct syntax provided below. This lets you replace older file versions in the archive with newer ones that have the same names, and add files that have been created in the root directory.
Compress-Archive -Path C:\path\to\files -Update -DestinationPath C:\path\to\archive.zip
And this concludes the process of the various scenarios that you can zip files using PowerShell in Windows 11/10. Continue below to see how you can unzip files using PowerShell.
Unzip files using PowerShell
As you have already seen, PowerShell can be used to zip files. The utility also can unzip archives. The process is even easier than compressing them – all you need is the source file and a destination for the data ready to unzip.
Let’s get to it.
To unzip files using PowerShell, do the following:
Open PowerShell using administrator privileges.
Next, type in the syntax below, replacing <PathToZipFile> and <PathToDestination> placeholder with the path to the files you want to compress and the name and folder you want it to go, respectively.
Expand-Archive -LiteralPath <PathToZipFile> -DestinationPath <PathToDestination>
The destination folder specified to extract the files into will populate with the contents of the archive. If the folder didn’t exist before unzipping, PowerShell will create the folder and place the contents into it before unzipping.
By default, if you leave out the -DestinationPath parameter, PowerShell will unzip the contents into the current root directory and use the name of the Zip file to create a new folder.
In this example, the folder Docs is specified in the command, so PowerShell will create the folder Docs in the path C:\Users\Chidum.Osobalu and extract the files from the archive into the folder. See the output folder containing the two files archived at the beginning of this post below.
Note that, if the folder Docs already exists in the destination, PowerShell will return an error when it tries to unzip the files. However, you can force PowerShell to overwrite the data with the new ones using the -Force parameter.
You should only use the -Force parameter if the old files are no longer needed, as this will irreversibly replace the files on your computer.
And this wraps up our subject on how to zip and unzip files using the PowerShell utility in Windows 11/10!
How do you zip and unzip files in Windows?
To zip or unzip files in Windows, you may use the built-in functionality of File Explorer, PowerShell, or any third-party file compression software, such as WinZip or 7-Zip. To zip files using Windows File Explorer, navigate to the location of the files you want to zip. Select the files, right-click on them, and select the Compress to ZIP file option. To unzip files, navigate to the zip archive in File Explorer, right-click on it, and select Extract All. Select a destination folder and click Extract to confirm your action.
How do I unzip a zip file in Windows command prompt?
Open Command Prompt using administrator rights. Use the cd
command to navigate to the directory where the zip file is located. Type tar -xf Name.zip -C C:\path\to\destination
in the Command Prompt window, while replacing the ‘Name’ parameter with the name of the zip file. Press Enter to execute the command. The content of the zip file will be extracted to the destination folder.
Read Next: How to install CURL on Windows 11.