Comprimare

How to Zip and Unzip files using PowerShell in Windows 10

How to Zip and Unzip files using PowerShell in Windows 10

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. In this post, we will show you how to zip and unzip files using the PowerShell utility in Windows 10.

How to zip files using PowerShell

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.

Do the following:

Press Windows key + X to open Power User Menu and then press I on the keyboard to launch PowerShell.

Next, type in the syntax below, replacing and 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  -DestinationPath 

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 and 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

Lastly, if you want an archive that only compresses files in the root directory and all its subdirectories, you will use the star-dot-star (*.*) wildcard to zip the files with the syntax below. With this method too, subdirectories and the files of the root folder 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 10. Continue below to see how you can unzip files using PowerShell.

Read: How to install CURL on Windows 10.

How to 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.

Next, type in the syntax below, replacing and 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  -DestinationPath 

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 10!

Cele mai bune jocuri Oculus App Lab
Dacă sunteți proprietarul căștilor Oculus, atunci trebuie să fiți informați despre încărcarea laterală. Încărcarea laterală este procesul de instalare...
Top 10 jocuri de jucat pe Ubuntu
Platforma Windows a fost una dintre platformele dominante pentru jocuri din cauza procentului imens de jocuri care se dezvoltă astăzi pentru a sprijin...
Cele mai bune 5 jocuri arcade pentru Linux
În zilele noastre, computerele sunt mașini serioase folosite pentru jocuri. Dacă nu puteți obține noul scor mare, veți ști la ce mă refer. În această ...