Home > OS >  Powershell one-liner not working from cmd
Powershell one-liner not working from cmd

Time:12-25

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.

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

Works for me with single quotes.

powershell using assembly IO.Compression.FileSystem; using namespace IO.Compression; [ZipFile]::ExtractToDirectory('c:/program/compressed.zip','c:/program')
  • Related