我想使用powershell在temp中的给定目录中提取所有.zip文件

Spi*_*776 4 powershell unzip

我编写了以下代码来将.zip文件解压缩到temp:

function Expand-ZIPFile($file, $destination)
{
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace($file)
    foreach ($item in $zip.items()) {
       $shell.Namespace($destination).copyhere($item)
    }
}

Expand-ZIPFile -file "*.zip" -destination "C:\temp\CAP"
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

PS C:\Users\v-kamoti\Desktop\CAP> function Expand-ZIPFile($file, $destination)
{
   $shell = new-object -com shell.application
   $zip = $shell.NameSpace($file)
   foreach ($item in $zip.items()) {
      $shell.Namespace($destination).copyhere($item)
   }
}

Expand-ZIPFile -file "*.zip" -destination "C:\temp\CAP"
You cannot call a method on a null-valued expression.
At line:5 char:19
+  foreach($item in $zip.items())
+                   ~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
Run Code Online (Sandbox Code Playgroud)

Jim*_*y R 11

Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force
Run Code Online (Sandbox Code Playgroud)

需要ps v5


小智 6

如果你想为每个 zip 文件创建一个新文件夹,你可以使用它:

#input variables
$zipInputFolder = 'C:\Users\Temp\Desktop\Temp'
$zipOutPutFolder = 'C:\Users\Temp\Desktop\Temp\Unpack'

#start
$zipFiles = Get-ChildItem $zipInputFolder -Filter *.zip

foreach ($zipFile in $zipFiles) {

    $zipOutPutFolderExtended = $zipOutPutFolder + "\" + $zipFile.BaseName
    Expand-Archive -Path $zipFile.FullName -DestinationPath $zipOutPutFolderExtended

    }
Run Code Online (Sandbox Code Playgroud)