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.

2
  • 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. Commented Sep 19, 2020 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. Commented Sep 20, 2020 at 17:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Which PowerShell version are you using in your machine(s) ? Run this cmdlet to check the PS version: Get-Host | Select-Object Version
I can't check it right now but it is a default version of windows 10
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.