Search code examples
powershellpowershell-2.0powershell-3.0

Load System.IO.Compression.FileSystem with Add-Type throws error


I want to extract a zip file using PowerShell. So, I use this:

Add-Type -AssemblyName System.IO.Compression.FileSystem

# some code to download the zip file from FTP #

[System.IO.Compression.ZipFile]::ExtractToDirectory($localpath+"\Test.zip",$localpath)

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

I then searched StackOverflow and changed

Add-Type -AssemblyName System.IO.Compression.FileSystem

to

Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.IO.Compression.FileSystem.dll"

It says:

Error: Could not load file or assembly 'file:///C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFram
ework\v4.6.1\System.IO.Compression.FileSystem.dll' or one of its dependencies. This assembly is built by a runtime newer
 than the currently loaded runtime and cannot be loaded.

I can actually see this dll in that v4.6.1 path. It is there. What is wrong with my syntax, please?


Solution

  • There is nothing wrong with your syntax; the problem is that the assembly is not compatible with your version of PowerShell - which is exactly what the second error says. For the first error, PowerShell might simply not have that path as part of the path that it searches for assembly DLLs because they're not compatible (again, see the error for the second attempt, where you specified the correct path for the assembly).

    If you can, you might want to consider updating your PowerShell (and Windows Management Framework). The current version of PowerShell is 5.