Extracting a ZIP file in Powershell

  • Follow


I have a function that I found that will extract the zip files in
powershell

I'm running this on a script on a remote machine that does not have
powershell 2.0

function Extract-Zip
{
param([string]$zipfilename, [string] $destination)

if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
}

Example to use this function is: extract-zip c:\temp\test.zip D:\
I have a couple questions with this

1) When I execute this function, it doesn't return an exit code. Is
there a way i can make the function force a exit code on completion or
failure?

2) When I run the function, if there are files located where I want to
copy them it will prompt me if i want to copy over them, and it gives a
option to click cancel while the files are being extracted.

Is there a way I can remove the cancel option and just force it to
overwrite any files that are there?

Thanks in advance


-- 
brambo23
0
Reply brambo23 5/25/2010 5:43:41 PM

Hello "brambo23".

"brambo23" wrote:
> I have a function that I found that will extract the zip files in
> powershell
>
> I'm running this on a script on a remote machine that does not
> have powershell 2.0
>
> function Extract-Zip
> {
> param([string]$zipfilename, [string] $destination)
>
> if(test-path($zipfilename))
> {
> $shellApplication = new-object -com shell.application
> $zipPackage = $shellApplication.NameSpace($zipfilename)
> $destinationFolder = $shellApplication.NameSpace($destination)
> $destinationFolder.CopyHere($zipPackage.Items())
> }
> }
>
> Example to use this function is: extract-zip c:\temp\test.zip D:\
> I have a couple questions with this
>
> 1) When I execute this function, it doesn't return an exit code.
> Is there a way i can make the function force a exit code on
> completion or failure?

No, the CopyHere method does not return anything.

> 2) When I run the function, if there are files located where I want
> to copy them it will prompt me if i want to copy over them, and it
> gives a option to click cancel while the files are being extracted.

You could try the second parameter of the CopyHere method:
$destinationFolder.CopyHere($zipPackage.Items(), 16)
http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx

-- 
Regards,
Wolfgang


0
Reply Wolfgang 5/27/2010 2:54:44 PM


On 5/27/2010 9:54 AM, Wolfgang Kais wrote:
> http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
>

Does anyone know when PowerShell examples are going to be added
by Microsoft to webpages like that one?

  - Larry
0
Reply Larry__Weiss 5/27/2010 4:51:12 PM

According to the helpfile 

http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx

CopyHere(vItem,[ vOptions ])

set Options to 16

"brambo23" wrote:

> 
> I have a function that I found that will extract the zip files in
> powershell
> 
> I'm running this on a script on a remote machine that does not have
> powershell 2.0
> 
> function Extract-Zip
> {
> param([string]$zipfilename, [string] $destination)
> 
> if(test-path($zipfilename))
> {
> $shellApplication = new-object -com shell.application
> $zipPackage = $shellApplication.NameSpace($zipfilename)
> $destinationFolder = $shellApplication.NameSpace($destination)
> $destinationFolder.CopyHere($zipPackage.Items())
> }
> }
> 
> Example to use this function is: extract-zip c:\temp\test.zip D:\
> I have a couple questions with this
> 
> 1) When I execute this function, it doesn't return an exit code. Is
> there a way i can make the function force a exit code on completion or
> failure?
> 
> 2) When I run the function, if there are files located where I want to
> copy them it will prompt me if i want to copy over them, and it gives a
> option to click cancel while the files are being extracted.
> 
> Is there a way I can remove the cancel option and just force it to
> overwrite any files that are there?
> 
> Thanks in advance
> 
> 
> -- 
> brambo23
> .
> 
0
Reply Utf 5/27/2010 7:57:20 PM

3 Replies
1684 Views

(page loaded in 1.47 seconds)

Similiar Articles:













8/1/2012 7:47:06 AM


Reply: