Zip and UnZip a file using PowerShell

Posted: August 12, 2014 in PowerShell scripts

Yes you can!
Having the option to fully automate your work is the goal for most PowerShell enthusiasts. One step closer – one step closer 🙂

To Zip a file you can use:

#zip function:
Function AddToZip ($Source, $Destination, [Switch]$includeTopFolder)
    {
    $outputcode0 = [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    $outputcode1 = [System.AppDomain]::CurrentDomain.GetAssemblies()
    $compressionLVL = [System.IO.Compression.CompressionLevel]::Optimal
    $outputcode2 = [System.IO.Compression.ZipFile]::CreateFromDirectory($Source, $Destination, $compressionLVL, $includeTopFolder)
    write-host -f Green "File $Destination created!"
    }

#Examples:
AddToZip -Source "C:\somefolder\folder" -Destination "C:\folder.zip" -includeTopFolder
AddToZip -Source "C:\somefolder\folder" -Destination "C:\folder.zip"

To unzip a file use:

#Unzip function:

function ExtractFromZip ($File, $Destination)
    {
    $ShellApp = new-object -com shell.application
    $zipFile = $ShellApp.NameSpace($File)
    foreach($item in $zipFile.items())
      {
      $ShellApp.Namespace($Destination).copyhere($item)
      }
    }

#Example:
ExtractFromZip –File "C:\lol.zip" –Destination "C:\temp\"
Advertisements
Comments
  1. Gary says:

    I’m trying to unzip a file stored in SharePoint Server 2013. I’ve tried using this script but can’t seem to make it work. Will the script work if the file is stored in SharePoint?

  2. Greateful User Browsing says:

    EXCELLENT THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s