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.
Open up Windows PowerShell and type in the
Compress-Archive Command following the below Syntax:
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
cdcommand and run the Command here too.
To Create a Compressed ZIP file with name 'assets.zip' of 'images' folder inside the cwd (Assuming that we have already navigated to the parent Folder of 'images')
Compress-Archive images assets
Similarly for a file named 'img.png', while creating 'images.zip' inside the 'dir' folder the command will look like this:
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.
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:
Compress-Archive @('./path/to/1st/resource', 'path/to/2nd/resource', ...) Compresed_File_name
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.
Use the Following Command to Exclude file/folder:
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-ChildItemdisplays the list of contents of the Directory.
-excludeflag followed by the resource name is used to exclude that resource from the list. This list is passed to the
Compress-ArchiveCommand through the pipeline
|that creates the ZIP file at the path specified after the
Get-ChildItem -exclude node_modules | Compress-Archive -DestinationPath app
You can also exclude multiple resources
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:
gci -exclude @('node_modules', 'assets/img') | Compress-Archive -DestinationPath app
Thank you for your time and attention!