I can't use the "system.io.compression.filesystem" in my powershell .NET Framework 4.7 has been installed. Any idea ?


if(Test-path $$ZipLog ) {Remove-item $$ZipLog}
Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]::CreateFromDirectory($ZipSourceDir, $ZipLog = "C:\MyBackup\test.zip")

PS C:\Windows\system32> C:\MyBackup\mytest.ps1
Add-Type : A positional parameter cannot be found that accepts argument '[io.compression.zipfile]::CreateFromDirectory'.
At C:\MyBackup\mytest.ps1:18 char:1
+ Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]:: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Type], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddTypeCommand
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
You need to add the type 1st
then use it:

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

$ZipLog = "C:\MyBackup\test.zip
#Cant create the variables within the function like C#. need to defined #before use it.

if(Test-path $ZipLog ) {Remove-item $ZipLog}

#In the compresion for 4.7 there is not ZipFile in that namespace.
[System.IO.compression.zipfile]::CreateFromDirectory($ZipSourceDir, $ZipLog )

Open in new window

And variables are single "$" not doubles, Also the approach was all wrong.

This is the final code:

#Addtypes (dll for compression and dll for ZipFile)
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.IO.Compression.dll"
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.IO.Compression.FileSystem.dll"

if(Test-path $ZipLog){
    Remove-item $ZipLog

Open in new window

Modify the optional parameters in line 3 and 4 accordingly.
Or use it as a script (Save that last one as test.ps1

And run it like:
test.ps1 -ZipSourceDir  "<sourcepath>" -ZipLog  "<zipfilepath\with\name.zip>"

I've published a script on Technet that will help you to understand this class


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.