0

I found PS script (below) for downloading ZIP file from ULR and unzipping it in specific file.

$Url = "www.link.com/test.zip"
$DownloadZipFile = "D:\Test" + $(Split-Path -Path $Url -Leaf)
$ExtractPath = "D:\Test"
Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile
$ExtractShell = New-Object -ComObject Shell.Application 
$ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items() 
$ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles) 
Start-Process $ExtractPath
#

This works fine but I have problem if same file already exists in this folder. So, I need to add if file exist to overwrite this file with new file. Can someone can help me?

Rgds, Nejc

  • Does this answer your question? `.CopyHere($ExtractFiles, 0x10)` https://stackoverflow.com/questions/2359372/how-do-i-overwrite-existing-items-with-folder-copyhere-in-powershell/5711383 – Mathias R. Jessen Nov 18 '21 at 10:14
  • Yes this works fine. Thanks a lot. –  Nov 18 '21 at 10:27

2 Answers2

1

you can check if file exist and delete it.

if (Test-Path $DownloadZipFile)
{
    Remove-Item -Path $DownloadZipFile
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0
$Url = "www.link.com/test.zip"
$DownloadZipFile = "D:\Test" + $(Split-Path -Path $Url -Leaf)
$ExtractPath = "D:\Test"
Invoke-WebRequest -Uri $Url -OutFile $DownloadZipFile
$ExtractShell = New-Object -ComObject Shell.Application 
$ExtractFiles = $ExtractShell.Namespace($DownloadZipFile).Items() 
$ExtractShell.NameSpace($ExtractPath).CopyHere($ExtractFiles, ***0x10***) 
Start-Process $ExtractPath
#
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '21 at 10:39