none
Ignore Prompts when using Unzip

    Question

  • Hello, I used the below Powershell script to unzip a folder and subfolders:

    $shell_app=new-object -com shell.application
    $filename = "Test.zip"
    $zip_file = $shell_app.namespace((Get-Location).Path + "\$filename")
    $destination = $shell_app.namespace((Get-Location).Path)
    $destination.Copyhere($zip_file.items(), 0x10)

    Where 0x10 is overwrite files, 0x4 is ignore dialog prompt, and 0x14 does both - overwrite and ignore the dialog box.
    The command works great except it still prompts a dialog box to replace all folders, even if I use any of the above examples??? I need to bypass any dialog prompts and just overwrite all files and folders.

    Tuesday, December 18, 2012 11:08 PM

Answers

  • The shell.application functionality for unzipping can be painful because even though it's scriptable, it wasn't really _designed_ for scriptability; there are lots of bits and pieces where you can run into roadblocks. The functionality also varies dramatically across different OS versions.

    In my case, running Windows 7 SP1, I don't encounter any problems using simply 0x10; I only get prompts if I omit the flags altogether, using

    $destination.Copyhere($zip_file.items())

    What OS version are you running, XP/2003? If that's the case, it's possible that the flags won't work at all for you. In that case, the following may be the best options for you to pursue.

    (A) Use an actual unzipping tool. The 7Zip tool, for example, ( http://www.7zip.org/ ) is free, fast, and has a commandline interface; and the DotNetZip tool ( http://dotnetzip.codeplex.com/ ) is a .NET library so you can use it natively within PowerShell once you load it.

    (B) If you can't use a tool similar to those, you might try this workaround:

    1. Make a temporary folder and set it as $destination. You can use a command like this to create a randomly named folder:
    $tempfolder = $env:temp + "\" + (Get-Random)
    md $tempfolder
    pushd $tempfolder
    $destination = $shell_app.namespace($tempfolder)

     2. Extract the files and folders to this new folder using $destination.CopyHere just as you did.

     3. Change back to the directory where you really want everything and perform a forced recursive move from the temp directory:

    popd
    Copy-Item -Path $tempfolder\* -Destination (Get-Location).Path -Container -Force
    

    4. Finally, delete the temporary folder:

     remove-item $tempfolder -Force -Recurse

    • Proposed as answer by Wieger1983 Wednesday, December 19, 2012 7:09 AM
    • Marked as answer by scifidad71 Wednesday, December 19, 2012 4:15 PM
    Wednesday, December 19, 2012 6:36 AM

All replies

  • The shell.application functionality for unzipping can be painful because even though it's scriptable, it wasn't really _designed_ for scriptability; there are lots of bits and pieces where you can run into roadblocks. The functionality also varies dramatically across different OS versions.

    In my case, running Windows 7 SP1, I don't encounter any problems using simply 0x10; I only get prompts if I omit the flags altogether, using

    $destination.Copyhere($zip_file.items())

    What OS version are you running, XP/2003? If that's the case, it's possible that the flags won't work at all for you. In that case, the following may be the best options for you to pursue.

    (A) Use an actual unzipping tool. The 7Zip tool, for example, ( http://www.7zip.org/ ) is free, fast, and has a commandline interface; and the DotNetZip tool ( http://dotnetzip.codeplex.com/ ) is a .NET library so you can use it natively within PowerShell once you load it.

    (B) If you can't use a tool similar to those, you might try this workaround:

    1. Make a temporary folder and set it as $destination. You can use a command like this to create a randomly named folder:
    $tempfolder = $env:temp + "\" + (Get-Random)
    md $tempfolder
    pushd $tempfolder
    $destination = $shell_app.namespace($tempfolder)

     2. Extract the files and folders to this new folder using $destination.CopyHere just as you did.

     3. Change back to the directory where you really want everything and perform a forced recursive move from the temp directory:

    popd
    Copy-Item -Path $tempfolder\* -Destination (Get-Location).Path -Container -Force
    

    4. Finally, delete the temporary folder:

     remove-item $tempfolder -Force -Recurse

    • Proposed as answer by Wieger1983 Wednesday, December 19, 2012 7:09 AM
    • Marked as answer by scifidad71 Wednesday, December 19, 2012 4:15 PM
    Wednesday, December 19, 2012 6:36 AM
  • I agree with Alex as to using option A. I've used CopyHere before. But there was something which did not work quite right, even when specifying flags. Though I ended up using something other than 7zip, I did use a third party utility of which I do not know the name anymore.
    Wednesday, December 19, 2012 7:08 AM
  • Thanks Alex!! I appreciate the help. I found that running this on Server 2003 I have the prompt issue, but if I run it on Server 2008 I do not.

    Thanks again,

    Scifdad71

    Wednesday, December 19, 2012 4:17 PM