Chuyển đến nội dung chính

How to Zip (and Unzip) Files Using PowerShell

PowerShellHeader

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. Here's how to zip and unzip files using PowerShell.

How to Zip Files Using PowerShell

Let's start off 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.

First, open PowerShell by searching for it from the Start menu and then typing in the following command, replacing <PathToFiles> and <PathToDestination> 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>

Zip up a few files in PowerShell.

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.

Note: 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 following command, replacing <PathToFolder>  and <PathToDestination> with the path to the files you want to compress and the name and folder you want it to go to, respectively:

Compress-Archive -LiteralPath <PathToFolder> -DestinationPath <PathToDestination>

Zip up an entire folder and all of its contents.

In the previous example, we put 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.

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.

Above, we covered 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, you tell PowerShell only to grab what's inside of the root directory. It should look something like this:

Compress-Archive -Path C:pathtofile* -DestinationPath C:pathtoarchive.zip

Zip up the entire contents of a folder, without the root folder itself.

Next, say you have a folder with a bunch of different file types (.doc, .txt, .jpg, etc.) but only want to compress all of one type. You can tell PowerShell to archive them without touching the others explicitly. The command's notation would look like this:

Compress-Archive -Path C:pathtofile*.jpg -DestinationPath C:pathtoarchive.zip

Zip up only specific file types from a folder.

Note: Subdirectories and the files of the root folder aren't included in the archive with this method.

Finally, if you want an archive that only compresses files in the root directory—and all its subdirectories—you would use the star-dot-star (*.*) wildcard to zip them. It would look something like this:

Compress-Archive -Path C:pathtofile*.* -DestinationPath C:pathtoarchive.zip

Zip up only files from the root folder with the use of the widlcard star-dot-star (*.*).

Note: Subdirectories and the files of the root folder aren't included in the archive with this method.

Even after the archive is complete, you can update an existing zipped file with the use of the -Update parameter. It 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. It will look something like this:

Compress-Archive -Path C:pathtofiles -Update -DestinationPath C:pathtoarchive.zip

Update an already existing zip file with the use of the -Update parameter.

How to Unzip Files Using PowerShell

In addition to being able to zip files and folders, PowerShell has the ability to 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.

Open PowerShell and type in the following command, replacing <PathToZipFile> and <PathToDestination> with the path to the files you want to compress and the name and folder you want it to go to, respectively:

Expand-Archive -LiteralPath <PathToZipFile> -DestinationPath <PathToDestination>

Unzip an archive with the Expand-Archive cmdlet.

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 the previous example, if we leave out -DestinationPath , PowerShell will create the folder "Archive" in the path "C:Usersbrady" and extract the files from the archive into the folder.

If you leave out the -DestinationPath parameter, PowerShell extracts the zip into its currently selected directory.

If the folder 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.

See details

Nhận xét

Popular Posts

Nicaraguan National Pleads Guilty to Conspiring to Distribute Cocaine and Marijuana on the Darknet

SACRAMENTO, Calif. — Eddy Steven Sandoval Lopez, 23, a Nicaraguan national residing in Sacramento, pleaded guilty today to conspiracy to distribute controlled substances, U.S. Attorney McGregor W. Scott announced. According to court documents, Sandoval Lopez distributed cocaine and marijuana on a darknet site, Dream Market, using the vendor accounts “CokeWave,” “SafeDealsDirect,” and “HerbanFarmer.” The Dream Market allowed individuals to sell narcotics and other illegal goods and services. In return for his cocaine and marijuana sales, Sandoval Lopez received $97,891 in bitcoin from his customers. This case is the product of an investigation by the Northern California Illicit Digital Economy Task Force (NCIDE), composed of agents from Homeland Security Investigations, the Federal Bureau of Investigation, the U.S. Postal Inspection Service, and the Drug Enforcement Administration. The NCIDE Task Force is a joint federal task force focused on targeting all forms of darknet and cryptocur...

E-Commerce Company Pleads Guilty To Antitrust Charge

Gennex Media and its President Sentenced for Conspiracy To Fix Prices of Promotional Products Sold Online Gennex Media LLC (aka Brandnex.com and PMGOA) pleaded guilty and was sentenced today for conspiring to fix prices for customized promotional products sold online to customers in the United States.   Gennex Media’s president, Akil Kurji, was also sentenced today for his role in the conspiracy. According to the felony charges filed on Nov. 1, 2018, and the plea agreement filed today in the U.S. District Court for the Southern District of Texas in Houston, Gennex Media, its president, Akil Kurji, and their co-conspirators agreed to fix the prices of customized promotional products sold online from May 2014 until at least June 2016.   The customized promotional products subject to the conspiracy included wristbands, lanyards, temporary tattoos, and buttons.   The defendants and their co-conspirators used social media platforms and encrypted messaging applications, such a...

Episode 177 - FSI2015 and Summer Plans

05/31/2015 It's Sunday, May 31st, 2015 and welcome to Episode 177 of TechTalk4Teachers, I'm Tom Grissom. Welcome to Episode 177 of the TechTalk4Teachers podcast, the show about teaching and learning with technology. I am starting out solo this month but we do have a pre-recorded segment with Cindy about our summer plans.  I am back from the #FSI2015 Conference and reflecting back on some of the approaches we take for teaching and learning. We end with our Technology Picks of the Week. Click on the Player to listen to this podcast Your browser does not support the audio element. If player does not work in your browser  Right Click Here to Download MP3 (38 minutes 55 seconds)  In this episode... I am freshly back from the Faculty Summer Institute 2015 Conference and share some of my thoughts about the conference. Cindy and I recorded a segment earlier this month about our summer plans that we share and we end with our Technology Picks of the Week. Show Notes Faculty ...