Powershell Extracting Zip

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.

1

1 Answer

Command Prompt (batch) processes the command and arguments first, so be careful with anything that could be interpreted as cmd symbols (double-quotes in your case), since cmd will remove them.

When running powershell commands, I prefer quoting the actual powershell part:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest