I recently came across a need to zip up some files prior to copying them over the network. Normally, I’d simply use another tool such as WinZip or 7zip, but I thought I’d like to see how PowerShell could handle it.
There are many tools out there to help you with this (7zip cli, PowerShell community extensions, etc). They’re not included with Powershell, so I decided to try and make use of the tools we have at hand.
I decided to go with a quick way to zip up files by leveraging .NET and using the cmdlet Add-Type
Note: This requires .NET 4.5 and Powershell 3+
This example script is pretty straightforward and simple. It makes use of the System.IO.Compression.Zipfile class that was included with .NET 4.5. We make use of the PowerShell cmdlet Add-Type to add a Microsoft .NET Framework type to the PowerShell session. This allows to leverage .NET in our PowerShell scripts.
Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?
Kris was an employee at PDQ.