unzip a file and copy it to different location

Hello,

i need to unzip a file and copy to different location…

sometimes its working under the local files but when i give shared location it doesnt…

please help me on this

param
(
[string]$spath,
[string]$destination

)

$files = Get-ChildItem $spath -include test*.zip -Recurse
foreach ($file in $files)

{

$archiveFile = $spath + $file.Name | out-string -stream

"Extracting the zip file to below location: " + $archiveFile
"Destination location : " + $destination
#$ErrorActionPreference = "SilentlyContinue"
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($archiveFile)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())

}

it throws the following error

You cannot call a method on a null-valued expression.
At L:\unzip.ps1:24 char:50

  • $destinationFolder.CopyHere($zipPackage.Items <<<< ())
    
    • CategoryInfo : InvalidOperation: (Items:String) , RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

Thanks in advance

When you get that error, what’s in your $archiveFile variable? One potential problem that stands out here is this:

$files = Get-ChildItem $spath -include test*.zip -Recurse

foreach ($file in $files) 
{
    $archiveFile = $spath + $file.Name | out-string -stream

You’re using the -Recurse switch for Get-ChildItem, which means that you could potentially be finding files such as “$spath\SubFolder\testSomething.zip”. However, when you set up your $archiveFile variable, it would just contain “$spath\testSomething.zip”, which probably wouldn’t exist. Instead, you can just say $archiveFile = $file.FullName , which will automatically have the full path to the file and avoid such problems. I don’t know if that’s what’s causing your current failure, though; you’d have to test and see.

Thanks Dave…its working now