2

I am using PowerShell 2.0 and can not update to a later version. I need to be able to compress a folder into a ZIP. I have scoured Stack Overflow and have found nothing that helps me.

This is one thing that I have tried and it throws an error.

Add-Type -Assembly "System.IO.Compression.FileSystem"
[io.compression.zipfile]::CreateFromDirectory("Source", "Destination")

I wanted this to compress the folder that I specified at "Source" and put it in the specified "Destination"

I got this error: Add-Type : Cannot add type. The assembly 'System.IO.Compression.FileSystem' could not be found.

AshMcSpec
  • 23
  • 1
  • 5

1 Answers1

1

Welcome to SO!

First, this is a possible duplicate of How to create a zip archive with PowerShell? - however, I believe I have an answer that is slightly better so I'll post it.

I suspect that the assembly not loading is a question of .NET framework installed rather than Powershell version, FWIW. You might like to see here.

The answer with the least amount of faffing is probably Jeff Hicks' post here. In case the link goes dark, I've abridged it to the bare bones:

Set-Content $OutputPath ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
$ZipFile = Get-Item $OutputPath
$ZipFile.IsReadOnly = $false  

$shell = New-Object -com shell.application
$zipPackage = $shell.NameSpace($zipfile.fullname)
$zipPackage.CopyHere($Path) 

For completeness, here's the equivalent unzipper:

$shell = new-object -com shell.application
$zip   = $shell.NameSpace($Path)
foreach($item in $zip.items())
{
    $shell.Namespace($Destination).CopyHere($item) | out-null
}

Excuse the wonky capitalisation - it makes my eyes hurt - but I'm sure you'll pretty it up as you please.

If this is a good answer, please mark it as such; otherwise, tell me what broke and I'll try to improve it.

FSCKur
  • 920
  • 7
  • 16