Powershell Extracting Zip
By Emily Wilson •
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.
11 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')"