Skip to content

Native compression in PowerShell

You can compress files and folders from PowerShell without installing third-party application (required .NET Framework 4.5):

[System.IO.Compression.ZipFile]::CreateFromDirectory(“$Source”, “$Archive”)

Here the function listing to use compression easy:

function Add-Zip ($Source, $Archive){
<#
.SYNOPSIS
Compression function

.DESCRIPTION
Native compression with latest .NET 4.5 framework

.PARAMETER Source
Source file or directory for compression

.PARAMETER Archive
Archive file name [with path]

.EXAMPLE
————————– EXAMPLE  —————————
Add-Zip -Source C:Temp -Archive C:Temp.zip

Compress C:temp folder to C:temp.zip archive

.LINK

https://makovetsky.me
#>
If (-Not $Source -or -Not $Archive) {
Write-Host ‘One parameter is missing’
} else {
Add-Type -Assembly “System.IO.Compression.FileSystem” ;
[System.IO.Compression.ZipFile]::CreateFromDirectory(“$Source”, “$Archive”) ;
}}

Published inMicrosoft

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.