PowerShell 5 – zip and unzip

One the extras in PowerShell 5.0 is a couple of cmdlets for workign with zip archives. Actually, you’ll find they are PowerShell advanced functions if you look in the module which you’ll find at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive

You get 2 cmdlets:

£> Get-Command *archive | ft CommandTYpe, Name -a

CommandType Name
———– —-
   Function Compress-Archive
   Function Expand-Archive

To compress

$files = Get-ChildItem -Path C:\Scripts -Filter *.csv | select -ExpandProperty Fullname
Compress-Archive -Path $files -DestinationPath C:\Scripts\t1.zip -CompressionLevel Optimal

or a single file

Compress-Archive -Path c:\scripts\test.csv -DestinationPath C:\Scripts\t2.zip -CompressionLevel Optimal


To uncompress

Expand-Archive -Path C:\Scripts\t1.zip  -DestinationPath c:\scripts

if you need to overwrite files:

Expand-Archive -Path C:\Scripts\t1.zip  -DestinationPath c:\scripts -Force

This entry was posted in PowerShell v5. Bookmark the permalink.

3 Responses to PowerShell 5 – zip and unzip

  1. John Oxley says:

    Because Get-ChildItem returns a list of FileInfo and DirectoryInfo objects, combined with Compress-Archive will process that in an intuitive way, you don’t need to select the full pathname out of the list of files. You can pipe the Get-ChildItem commandlet straight to the Compress-Archive commandlet.

    gci -path C:\Scripts -Filter *.csv | Compress-Archive -DestinationPath C:\Scripts\t1.zip

  2. Sanjay says:

    I have a different requirement. need to compress each zip files based on selected file names. Input file would be an CSV format with below.
    ZipFileName | FilestoZip
    EnggA.zip | report.pdf, qa.pdf, draft.pdf
    ProdB.zip | Prod2.pdf, materila.pdf | inventory.pdf

    How Compress-Archive can read and loop thru to create each zip file selecting those file from the same source directory?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s