Compressing to ZIP Files

by Jan 28, 2016

In PowerShell 5.0, Compress-Archive can easily compress files and folders to a ZIP file:

 
PS C:\> Compress-Archive -Path c:\sourcefolder -DestinationPath $env:temp\archive.zip -Force 

Compress-Archive is not a cmdlet, though. It is a function and leverages the new ZIP file support in .NET Framework 4.5. To take a look at how they did this, here is a line that copies the Compress-Archive source code to the clipboard.

 
PS C:\> ${function:Compress-Archive} | clip

You can use this approach to view the source code of any PowerShell function that is in memory. You may have to run a function once to force PowerShell to load it into memory before you can access its source code.

When you create a ZIP file with Compress-Archive (requires PowerShell 5.0), any file with the “Hidden” attribute is not added to the ZIP file by the way.

 

Throughout this month, we’d like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!