Windows OS Hub
  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Group Policies
  • Windows Clients
    • Windows 10
    • Windows 8
    • Windows 7
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
  • PowerShell
  • Exchange

 Windows OS Hub / PowerShell / How to Compress & Extract ZIP Archive Using PowerShell

March 9, 2016 PowerShell

How to Compress & Extract ZIP Archive Using PowerShell

Usually to create archives automatically, I use 7zip. This tool is free, convenient and has a command line interface, which allows to use it in scripts. However, it is not always possible to install a third-party software on a server, so, if necessary, ZIP archives can be created using the built-in Windows features.

To create a ZIP archive, you can use ZipFile class that appeared in .NET Framework 4.5. This class is not loaded  by default, so the first thing you have to do is to add the assembly using this command:

Add-Type -Assembly “system.io.compression.filesystem”

After adding the assembly, you can create archives. To do it, use CreateFromDirectory statistic method of ZipFile class. To create an archive from a directory with this method, you have to specify a source directory and the target ZIP-file name as arguments. For instance:

$source = “C:\PoSh\TestZip”
$destination = “C:\PoSh\test.zip”
[io.compression.zipfile]::CreateFromDirectory($source, $destination)

Create Zip Archive with PowerShell

You can set the level of compression in CreateFromDirectory method:

  • Optimal — optimization by the level of compression
  • Fastest — optimization by the time taken
  • NoCompression — without any compression

You can also specify if the archive has to be included into the base directory. As an example, let’s create another archive with Optimal level of compression and not include it into the base directory:

$level = [System.IO.Compression.CompressionLevel]::Optimal
$Include = $false
[io.compression.zipfile]::CreateFromDirectory($source, $destination, $level, $Include)

When creating an archive, remember that this method cannot rewrite the existing files and if there is a ZIP file with this name in the directory, it returns an error.

PowerShell: ZIP Archive CompressionLevel

The reverse operation is decompression. To do it, use ExtractToDirectory method. To run it, you have to specify a source ZIP file and a directory to extract it to. As an example, let’s decompress the archive created earlier:

$source = ″C:\PoSh\test.zip″
$destination = ″C:\PoSh\TestZip1″
[io.compression.zipfile]::ExtractToDirectory($source, $destination)

Extract ZIP files using PoSh

That’s all, and you can learn more about ZipFile class of .Net, its properties and methods from MSDN library.

0 comment
0
Facebook Twitter Google + Pinterest
previous post
VMware Converter: Synchronize changes when perfoming P2V or V2V
next post
How To Configure DHCP Server Using PowerShell

Related Reading

How to Sign a PowerShell Script (PS1) with...

February 25, 2021

Configuring PowerShell Script Execution Policy

February 18, 2021

Updating Group Policy Settings on Windows Domain Computers

February 16, 2021

How to Find Inactive Computers and Users in...

January 29, 2021

Checking User Logon History in Active Directory Domain...

January 22, 2021

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange
  • Windows 10
  • Windows 8
  • Windows 7
  • Windows Server 2016
  • Windows Server 2012 R2
  • Windows Server 2008 R2
  • PowerShell
  • VMWare
  • MS Office

Recent Posts

  • Accessing USB Flash Drive from VMWare ESXi

    February 26, 2021
  • How to Sign a PowerShell Script (PS1) with a Code Signing Certificate?

    February 25, 2021
  • Change the Default Port Number (TCP/1433) for a MS SQL Server Instance

    February 24, 2021
  • How to Shadow (Remote Control) a User’s RDP session on RDS Windows Server 2016/2019?

    February 22, 2021
  • Configuring PowerShell Script Execution Policy

    February 18, 2021
  • Configuring Proxy Settings on Windows Using Group Policy Preferences

    February 17, 2021
  • Updating Group Policy Settings on Windows Domain Computers

    February 16, 2021
  • Managing Administrative Shares (Admin$, IPC$, C$, D$) in Windows 10

    February 11, 2021
  • Packet Monitor (PktMon) – Built-in Packet Sniffer in Windows 10

    February 10, 2021
  • Fixing “Winload.efi is Missing or Contains Errors” in Windows 10

    February 5, 2021

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • How Automatically Fill Computer Description Field in Active Directory
  • Hyper-V PowerShell Direct in Windows Server 2016
  • Run MySQL Queries from PowerShell
  • PowerShell Remoting via WinRM for Non-Admin Users
  • Adding a Simple Colorful Menu to PowerShell Script
  • How To Configure DHCP Server Using PowerShell
  • How to Access and Manage Windows Registry with PowerShell
Footer Logo

@2014 - 2018 - Windows OS Hub. All about operating systems for sysadmins


Back To Top