Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to extract all .zip files in a given directory in temp using powershell

I wrote the following code for extracting the .zip files to 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"

But I got the following error:

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
like image 703
Spidy776 Avatar asked Feb 11 '15 06:02

Spidy776


1 Answers

Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force

requires ps v5

like image 53
Jimmy R Avatar answered Oct 14 '22 16:10

Jimmy R