0

I have the following .ps1 for unzipping a zip file...

param([string]$path)
$shell=new-object -com shell.application
$Location=$shell.namespace($path)
$ZipFiles = get-childitem *.zip

get-childitem $path -include *.xml -recurse | foreach ($_) {remove-item $_.fullname}

foreach ($ZipFile in $ZipFiles)
{
    $ZipFile.fullname | out-default
    $ZipFolder = $shell.namespace($ZipFile.fullname)
    $Location.Copyhere($ZipFolder.items())
}

and the following run.bat file for setting the parameter and calling the .ps1

 powershell -command "C:\Users\eric\unzipFile\unzip3.ps1 -path \"C:\Users\eric\unzipFile\""

If both are in the same directory, no error, however if I move the run.bat to another directory I get the following...

You cannot call a method on a null-valued expression.
At C:\Users\eric\unzipFile\unzip3.ps1:12 char:38
+ $Location.Copyhere($ZipFolder.items <<<< ())
+ CategoryInfo: InvalidOperation: (items:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
2
  • 1
    It's probably this $ZipFiles = get-childitem *.zip. Add Write-Host $pwd before that to see where it's looking for *.zip. Commented May 21, 2013 at 20:34
  • Thank you, was looking in the current directory instead of the path handed to it Commented May 21, 2013 at 20:42

1 Answer 1

2

You didn't specify in which path to look for zip files, so it looks in the current folder. Change it as follow:

$ZipFiles = get-childitem -Path $path -Filter *.zip

Other tip: Use the -File parameter from powershell and add other parameters to make it easier to call

powershell -file "C:\Users\eric\unzipFile\unzip3.ps1" -path "C:\Users\eric\unzipFile\"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.