0

When I run this command in a normal PowerShell window it works fine:

Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory("c:/program/compressed.zip","c:/program")

However when I run it as:

powershell Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory("c:/program/compressed.zip","c:/program")

or

cmd /c powershell Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory("c:/program/compressed.zip","c:/program")

I get this error:

At line:1 char:119
+ ... System.IO.Compression.ZipFile]::ExtractToDirectory(c:/program/compressed.z ...
                                                                  ~
You must provide a value expression following the '/' operator.
At line:1 char:119
+ ... System.IO.Compression.ZipFile]::ExtractToDirectory(c:/program/compressed.z ...
                                                                 ~
Missing ')' in method call.
At line:1 char:119
+ ... Compression.ZipFile]::ExtractToDirectory(c:/program/compressed.zip,c:/progr ...
                                                  ~~~~~~~~~~
Unexpected token 'compressed.zip' in expression or statement.

How can I make it work when adding PowerShell in front?

Thank you for your time.

Peter2223
  • 5
  • 1
  • 1
    You may (re-)read the help you get when you run `PowerShell /?`. Especially for the parameter `-Command` ;-) – Olaf Dec 24 '21 at 16:33
  • 1
    This might help as well: [PowerShell CLI fundamentals:](https://stackoverflow.com/a/68136129/9196560) – Olaf Dec 24 '21 at 16:35
  • Adding {} fixed it when running from a powershell window but it still doesn't work when calling from cmd – Peter2223 Dec 24 '21 at 17:01
  • please mark the answer that solved your problem `as answer` so you could help other people – Mahmoud Moawad Dec 31 '21 at 23:17

3 Answers3

1

Works for me

powershell -command "& {Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory(\"c:/program/compressed.zip\",\"c:/program\")}"

from powershell /? we get -command "& { < command > }"

but the enclosed " are poison to the outer quotes thus need \" escaping

and now you have several alternate answers to avoid those internal "

NOTE this is the only native way to unzip on Windows 7 where I tested, but on Windows 10/11 I would use the simpler TAR to extract

TAR -m -xf compressed.zip if it was run in that folder or

cd /d c:\program & TAR -m -xf compressed.zip (other options are available as shown in answer

However TAR --help will not show you how to zip (tar -a) although it is possible. for basic choices see here https://stackoverflow.com/a/70011759/10802527 but you can use TAR to navigate between folders collecting files as it goes into one zip. What you may not be able do easily with TAR is combine (append) zips without temporary out's and in's.

K J
  • 8,045
  • 3
  • 14
  • 36
1

This is how it works with me

powershell -c "Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory('c:/program/compressed.zip','c:/program')"

you just need to replace the double quotes with single quotes of the pathes

Mahmoud Moawad
  • 697
  • 7
  • 14
0

Works for me with single quotes.

powershell using assembly IO.Compression.FileSystem; using namespace IO.Compression; [ZipFile]::ExtractToDirectory('c:/program/compressed.zip','c:/program')
js2010
  • 23,033
  • 6
  • 64
  • 66