# unzip a file and copy it to different location

**URL:** <https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398>\
**Category:** PowerShell Help\
**Created:** [November 10, 2014, 12:40pm UTC](https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398 "2014-11-10T12:40:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat-musunuri](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@venkat-musunuri](https://forums.powershell.org/u/venkat-musunuri)\
**Post date:** [November 10, 2014, 12:40pm UTC](https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398/1 "2014-11-10T12:40:48Z")

</div>

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 &lt;&lt;&lt;&lt; ())

```

  - CategoryInfo : InvalidOperation: (Items:String) , RuntimeException
  - FullyQualifiedErrorId : InvokeMethodOnNull

Thanks in advance

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 11, 2014, 1:59am UTC](https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398/2 "2014-11-11T01:59:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![venkat-musunuri](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@venkat-musunuri](https://forums.powershell.org/u/venkat-musunuri)\
**Post date:** [November 11, 2014, 3:28am UTC](https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398/3 "2014-11-11T03:28:25Z")

</div>

Thanks Dave…its working now

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/unzip-a-file-and-copy-it-to-different-location/3398/4 "2024-05-16T20:46:43Z")

</div>


