0

When I try to perform command

 Invoke-WebRequest -Uri example.com/zip.zip -OutFile C:\SomePath\zip.zip

PowerShell just thinks for few seconds and then exits, PowerShell window is just vanishing. But when i enter just Invoke-WebRequest, it asks me for parameters but effect is same when i input parameters one by one.

Gyanbu
  • 33
  • 1
  • 8
  • Invoke-WebRequest is intended to fetch content from a "Web page" on the Internet. What exactly are you trying to achieve using the above cmdlet? Please provide some clarifications on your requirement. – Yash Gupta Sep 19 '20 at 17:58
  • 1
    I just want to download file from github using PowerShell, I know it works becouse i checked it in clean windows 10 vitrual maschine and it worked. – Gyanbu Sep 20 '20 at 17:16

1 Answers1

3

In order to download a file, Invoke-WebRequest isn't the most optimal way as the HTTP response stream is buffered into memory, and once the file has been fully loaded- then only it will be flushed to disk. This can cause a performance impact in case of large files.

I would suggest you to use the System.Net.WebClient DotNET class to download files from your GitHub source. You can refactor your code to something like this:

$url = "http://github.com/zip.zip"
$output = "C:\SomePath\zip.zip"
(New-Object System.Net.WebClient).DownloadFile($url, $output)

How is this cmdlet better than Invoke-WebRequest? You might ask.

With System.Net.WebClient, the speed/performance gets improved a lot as the HTTP response stream is buffered directly to disk throughout the download process (and not splitting the work into fetch-and-flush tasks).

Note: Make sure the local output file (for which you're providing the path in $output) is a valid file and it exists, or else you might get some error while using DownloadFile method.

UPDATE:

Since the above solution doesn't seem to be working as expected in case of compressed files, here's another workaround that can be used for achieving this using PowerShell:

$url = "http://github.com/zip.zip" 
$zipOutput = "C:\ZipOutput\" + $(Split-Path -Path $url -Leaf) 
$extractedOutput = "C:\ExtractedOutput\"
(New-Object System.Net.WebClient).DownloadFile($url, $zipOutput)
$shellObj = New-Object -ComObject Shell.Application 
$files = $shellObj.Namespace($zipOutput).Items() 
$shellObj.NameSpace($extractedOutput).CopyHere($files) 
Start-Process $extractedOutput

The zip file will be downloaded to the path provided in $zipOutput, and the script will further extract the contents & store extracted contents in the path provided in $extractedOutput. Make sure that the 'C:\ZipOutput' and 'C:\ExtractedOutput' folders exist on your machine where you're executing this script.

Yash Gupta
  • 1,807
  • 15
  • 21
  • Thanks for that much help from you but it's not it. I tried to download any file .exe . zip .msi but it just don't work, and to unpack zip I am just using Expand-Archive command. Command Help works so it's not a internet/firewall issue. Idk is there a way that you can block some functions of Power Shell? If no is there a way to check PowerShell files integrity/reinstalling it? I don't know anymore. – Gyanbu Sep 22 '20 at 21:41
  • Which PowerShell version are you using in your machine(s) ? Run this cmdlet to check the PS version: `Get-Host | Select-Object Version` – Yash Gupta Sep 23 '20 at 16:55
  • I can't check it right now but it is a default version of windows 10 – Gyanbu Sep 24 '20 at 15:11
  • Version of PowerShell 5.1.19041.1 And i also discovered that you need elevated PowersShell to download file using ``(New-Object System.Net.WebClient).DownloadFile(url, output)`` – Gyanbu Sep 26 '20 at 12:27