Compress Folders like a PRO (Window)
Tags: zip, productivity, archive, powerfuldevs
Published: 2021-09-28 06:21:15

Bored of using the GUI to Compress the folders? Then you have come to the right Blog that will change the way you Compress your folders/files forever. Today, I will be covering how to use Windows PowerShell to Compress the Folder/File and also exclude or Include specific folders/files while Compressing folders.

---
###Simple Compression Operation

Open up Windows PowerShell and type in the `Compress-Archive` Command following the below Syntax:

```powershell
Compress-Archive path/to/folder/file Compresed_File_name
```

> Path to the Folder/File can be both Absolute or Relative. You can also navigate to the Parent Folder of the File or Folder you want to Compress using the `cd` command and run the Command here too.

#### Examples

1. To Create a Compressed ZIP file with name 'assets.zip' of 'images' folder inside the <abbr title='Current Working Directory'>cwd</abbr> (Assuming that we have already navigated to the parent Folder of 'images')

    ```powershell
    Compress-Archive images assets
    ```

2. Similarly for a file named 'img.png', while creating 'images.zip' inside the 'dir' folder the command will look like this:

    ```powershell
    Compress-Archive img.png dir/image
    ```
---

Now, You will be like, Aizaz, WTH, What's Interesting in this? I can like get the above work done in just 2 clicks and you are asking me to write these Scary Commands to get the work done. Yes, you are right but these are just the basic stuff. I will be now explaining more interesting and amazing parts of it. 

---

### Creating a Single ZIP file from Multiple Folders/Files

Using the above same command, We can also create a single ZIP file from multiple resources (Folders/Files) by using the command with the following syntax:

```powershell
Compress-Archive @('./path/to/1st/resource', 'path/to/2nd/resource', ...) Compresed_File_name
```
For Example: 
```powershell
Compress-Archive @('file.txt', 'folder-1', 'folder-2') dir/example
```
This will create a'example.zip' file inside the 'dir' folder with file.txt, folder-1, and folder-2 compressed.

> This above Syntax can be used to choose specific files/folders inside a folder to be included in the archive. Just navigate to the folder containing the content you want to Compress and specify the files you want to include in the ZIP file.

---

### Creating ZIP file Excluding Specific files/folders
There comes many situations where one has to exclude particular files/folders while creating a ZIP file. One kind of situation is when one has to send the Archive (Zip File) of a JavaScript Application that has node_modules installed, but one can't afford node_modules to be included inside a ZIP file as it will be troublesome to send such a huge file over the internet. One way of Getting Rid of this is to create a new folder and place all files other than node_modules in it and then compress that folder and send it. It Works, but this practice becomes weary when you have to do that again and again after changes in Source Code. But, now that's not the problem anymore.
Use the Following Command to Exclude file/folder:
```powershell
Get-ChildItem -exclude file/folder_to_be_excluded | Compress-Archive -DestinationPath Compresed_File_name
```
> Run this Command after navigating to the directory you want to Compress. `Get-ChildItem` displays the list of contents of the Directory. 
`-exclude` flag followed by the resource name is used to exclude that resource from the list. This list is passed to the `Compress-Archive` Command through the pipeline `|` that creates the ZIP file at the path specified after the `-DestinationPath` command.
#### Examples
```powershell
Get-ChildItem -exclude node_modules | Compress-Archive -DestinationPath app
```
You can also exclude multiple resources
```powershell
Get-ChildItem -exclude @('node_modules', 'assets/img') | Compress-Archive -DestinationPath app
```
We can also use an alias for `Get-ChildItem` command to make it simpler:
```powershell
gci -exclude @('node_modules', 'assets/img') | Compress-Archive -DestinationPath app
```
> You can also Check out the documentation of both [Get-ChildItem](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem) and [Compress-Archive](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/compress-archive) Commands to get detailed insight of these.

**Thank you for your time and attention!**