Powershell 提取 Zip

Powershell 提取 Zip

当我在普通 PowerShell 窗口中运行此命令时,它运行正常:

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

但是当我按如下方式运行它时:

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

或者

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

我收到此错误:

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.

在前面添加 PowerShell 怎样才能使其工作?

感谢您的时间。

答案1

命令提示符(批处理)首先处理命令​​和参数,因此要小心任何可能被解释为 cmd 符号(在您的情况下为双引号)的内容,因为 cmd 会删除它们。

运行 powershell 命令时,我更喜欢引用实际的 powershell 部分:

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

相关内容