Files or folders in PowerShell can be archived, zipped, or compressed by utilizing the “Compress-Archive” cmdlet. Moreover, it can also create a new archive file from scrape. PowerShell uses the “-CompressionLevel” parameter to apply compression to a file archiving a file. The higher the compression level will consume the lesser time to create a file. However, doing so will increase the file size.
In this article, we will provide information about the “Compress-Archive” cmdlet.
What is Compress-Archive Cmdlet in PowerShell?
As it is described above, the stated cmdlet is responsible for archiving single or multiple files. Let us discover the ways to compress a file into a zip file with the aid of the examples given.
Example 1: Using the “Compress-Archive” Cmdlet to Compress Two Files
Execute the code mentioned below to archive more than one file:
In accordance with the above code:
- First, specify the “Compress-Archive” cmdlet along with the “-LiteralPath” parameter having a path of the two files separated by a comma.
- Next, place a “-CompressionLevel” parameter and assign it the “Optimal” value.
- Lastly, add the “-DestinationPath” parameter, provide the destination path and extension:
Example 2: Compress a Whole Directory to a Zip File
To compress a folder into a zip file, execute the following command:
In the above-stated code:
- First, add the “Compress-Archive” cmdlet along with the “-Path” parameter having the specified path assigned to it.
- Next, write the “-DestinationPath” parameter and provide the target path:
Example 3: Create a Zip File Containing Multiple Files From a Folder with the Aid of the Wildcard
To compress a complete folder containing several by placing the wildcard “*” character alongside the folder path, use the provided command:
Example 4: Update an Existing Zip File Using the “-Update” Parameter
Add the “-Update” parameter along with the “Compress-Archive” cmdlet to update the existing zip file:
That’s it! We have discussed the “Compress-Archive” cmdlet in detail.
Conclusion
The “Compress-Archive” cmdlet is responsible for archiving, zipping, or compressing a file or a directory in PowerShell. Compressing a file protects the file from malicious activity. This tutorial provided detail about the “Compress-Archive” cmdlet in PowerShell.