unzip a file and copy it to different location

This topic contains 2 replies, has 2 voices, and was last updated by  Robin16 3 years, 7 months ago.

  • Author
  • #20491



    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



    $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)

    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

  • #20492

    Dave Wyatt

    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.

  • #20498


    Thanks Dave...its working now

You must be logged in to reply to this topic.