EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell ZIP
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell ZIP

PowerShell ZIP

Definition of PowerShell ZIP

PowerShell ZIP module allows to compress the files and the folders on the local computer or the remote machine without installing any third party or inbuilt software and zip module (Microsoft.PowerShell.ZIP) ships with the two cmdlets Compress-Archive and Expand-Archive among them, the Compress-Archive cmdlet is used to compress the files and folders and there is also System.IO.Compression class of the .Net Framework is used for compressing the data on the windows systems.

Syntax:

Compress-Archive Syntax:

Syntax 1:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax 2:

Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] -Update
[-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax 3:

Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] -Force
[-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax 4:

Compress-Archive
-LiteralPath <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] -Update
[-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax 5:

Compress-Archive
-LiteralPath <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] -Force
[-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Syntax 6:

Compress-Archive
-LiteralPath <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>] [-PassThru] [-WhatIf] [-Confirm] [<CommonParameters>]

Among the given syntaxes sets, we can use only one syntax combination at a time. For example, a Combination of Syntax4 and Syntax5 cannot be used together that is we can’t use -Force and -Update parameters together.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,294 ratings)

System.IO.Compression Syntax: 

ZipFile.CreateFromDirectory: This class methods create a Zip archive from the Directory.
ZipArchive.CreateEntry: This class methods add new files to an existing Zip archive.

How does the ZIP function work in PowerShell?

The easy way to zipping the files and folders is by using the Compress-Archive cmdlet. This cmdlet was introduced in the PowerShell module Microsoft.PowerShell.Archive in PS version 5. It uses the Source path and the destination to compress the data and also uses the different compression levels.
Compression Levels specify how much compression we to apply to create a ZIP file. A faster compression level compresses the file faster but leads to larger file size. There are three types of compressions.

a. Fastest: Uses the fastest compression method to compress a file and it reduces the processing time but the file size will be larger.

b. NoCompression: Doesn’t apply any compression method and it doesn’t compress the source file. The destination archive file will be larger than the Optimal compression.

c. Optimal: This is the moderate level and default for this command. The processing time depends on the size of the file.

When we use the .NET compression API System.IO.Compression.ZipArchive, it supports the below overload constructors to create a Zip file.

a. CreateFromDirectory(String, String): Creates a Zip archive that contains the files and the folders from the specified directory.

b. CreateFromDirectory(String, String, CompressionLevel, Boolean): It creates a ZIP archive that contains the files and folders from the specified directory, uses the compression level, and optionally includes the base directory.

c. CreateFromDirectory(String, String, CompressionLevel, Boolean, Encoding): It creates a zip archive that contains the files and folders from the specified directory, uses the compression level, optionally includes the base directory, and uses the character encoding for the entry name.

We will understand more about the above methods when we see the examples.

Examples

Let us discuss examples of PowerShell ZIP.

Example #1: Using Compress-Archive to create an archive file.

In this example, we are archiving two files at the destination location ‘C:\Archived Data’ with the default compression level (Although specified) and it creates a destination file name Compressed.Zip.

Compress-Archive -Path C:\Temp\10vms.csv, C:\Temp\ADUserData.csv `
-DestinationPath 'C:\Archived Data\Compressed.zip' `
-CompressionLevel Optimal -Force -Verbose

Output:

PowerShell ZIP 1

Example #2: Compress-Archive with Splatting.

We can also use the first example with splatting, which gathers requires input into a single hashtable and pass it to the command as shown below.

$data = @{
Path = 'C:\Temp\10vms.csv','C:\Temp\ADUserData.csv'
DestinationPath = 'C:\Archived Data\Compressed.zip'
CompressionLevel = 'Optimal'
Force = $true
Verbose = $true
}
Compress-Archive @data

The above command is similar to Example 1.

Example #3: Compress-Archive to create a source folder archive.

To zip the specific folder, we need to use the wildcard character (*) so the entire folder and files will be archived as shown below. We will use the fastest compression level here.

$data = @{
Path = 'C:\Temp\*'
DestinationPath = 'C:\Archived Data\temparchive.zip'
CompressionLevel = 'Fastest'
Force = $true
Verbose = $true
}
Compress-Archive @data

We can also use the wildcard (*) for the specific file name that starts with a specific word. For example, we can use C:\temp\ts* so it will take all the files starting with TS and C:\temp\*.csv to compress only CSV files from the C:\temp folder.

Example #4: Compress-Archive with the NoCompression level.

When we use the NoCompression level to create an archive, its compression ratio would be 0 and results in increasing destination folder size. For example,

$data = @{
Path = 'C:\Temp\*.csv'
DestinationPath = 'C:\Archived Data\CSVarchive1.zip'
CompressionLevel = 'NoCompression'
Force = $true
Verbose = $true
}
Compress-Archive @data

When we check the compression ratio it would be 0. See the output below.

PowerShell ZIP 2

Example #5: Update an Existing file.

The below command will add new files and folders from the C:\temp and also add the C:\Test1\7z1900-x64.msi to the existing archive temparchive.zip.

$data = @{
Path = 'C:\Temp\*', 'C:\Test1\7z1900-x64.msi'
DestinationPath = 'C:\Archived Data\temparchive.zip'
Update = $true
Verbose = $true
}
Compress-Archive @Data

Example #6: Using .Net Namespace method.

With the System.IO.Compression.ZipFile class and CreateFromDirectory method, we can create the compressed file as shown below. It creates an archive of the C:\temp to the destination path “C:\Archived Data\DotNetCompression.Zip”.

[System.IO.Compression.ZipFile]::CreateFromDirectory('C:\temp\', 'C:\Archived data\Dotnetcompression.zip')

We can also add the CompressionLevel from its syntax CreateFromDirectory(String, String, CompressionLevel, Boolean).

Add-Type -AssemblyName System.IO.Compression.FileSystem
$CompressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
[System.IO.Compression.ZipFile]::CreateFromDirectory('C:\temp','C:\Archived Data\Dotnetcompression.zip',$CompressionLevel,$true)

In the above example, we are using the Compression Level faster and $true the last argument mentions the includeBaseDirectory property which mentions $true means Include the directory name from the source directory from the root of the archive, and $false means to include only the content of the directory.

Conclusion

PowerShell provides a great way to compress the data without using any software, and we just need PowerShell modules. This helps us greatly when we can’t install ZIP software in restricted systems. There is also third-party software like 7ZIP which offers the PowerShell module to ZIP and unzip files.

Recommended Articles

This is a guide to PowerShell ZIP. Here we discuss the Definition, syntax, How does the ZIP function work in PowerShell? examples with code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell Wait
  2. PowerShell change directory
  3. PowerShell where
  4. PowerShell uninstall module
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more