Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell unzip error

Tags:

powershell

I got a following script from the internet, when I tried to run it gives me an error.

#script
#unzip folder

$shell_app = new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace("C:\temp\$filename")

#set the destination directory for the extracts
$destination = $shell_app.namespace("C:\temp\zipfiles")

#unzip the file
$destination.Copyhere($zip_file.items())

--- Error Message

    You cannot call a method on a null-valued expression.
At line:1 char:22
+ $destination.Copyhere <<<< ($zip_file.items())
    + CategoryInfo          : InvalidOperation: (Copyhere:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
like image 690
Lakhae Avatar asked Jan 23 '13 22:01

Lakhae


1 Answers

you need to create the "c:\temp\zipfiles" folder before you set the $destination variable, this kind of sloppy way but it will do the job :)

#script
#unzip folder

$shell_app = new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace("C:\temp\$filename")

#set the destination directory for the extracts
if (!(Test-Path "C:\temp\zipfiles\")) { 
    mkdir C:\temp\zipfiles
}
$destination = $shell_app.namespace("C:\temp\zipfiles")

#unzip the file
$destination.Copyhere($zip_file.items())
like image 59
OhadH Avatar answered Oct 16 '22 08:10

OhadH