fbpx

PowerShell WGet: Learn How To Download a File

In today’s digital world, downloading files is common for most computer users. While many tools are available to help with this task, PowerShell is a powerful tool that can make the process even more efficient.

PowerShell is a command-line shell and scripting language developed by Microsoft that allows users to automate tasks on Windows operating systems. 

In this article, we will explore how to use PowerShell to download a file from the internet, and how this can be useful in various scenarios. Whether you are a seasoned IT professional or a casual computer user, understanding how to use PowerShell to download files can be valuable in your toolkit.

Requirements to Download A File Using PowerShell

Before we get started, it is important for the users to have the following factors in order to download a file with PowerShell – 

Methods to Download a File through PowerShell

  1. Invoke-WebRequest
    • Finding Parsing Errors
  2. Invoke-RestMethod
  3. Start-BitsTransfer
    • Downloading one File
    • Downloading several Files
  4. WebClient Class and HttpClient Class (.NET Framework)
    • System.Net.WebClient
    • System.Net.Http.HttpClient

Using PowerShell to Download a File

Invoke-WebRequest

If you’re looking for an alternative to the popular wget command for downloading files in PowerShell, then the Invoke-WebRequest cmdlet might be just what you need. Invoke-WebRequest is a powerful command that allows you to interact with websites and web services, and it can be used to download files as well.

To use Invoke-WebRequest to download a file, you simply need to specify the URL of the file you want to download and the path where you want to save it. For example, the following command will download a file from the specified URL and save it to the specified path:

Invoke-WebRequest -Uri https://example.com/file.zip -OutFile C:\Downloads\file.zip
Invoke-WebRequest

You can also use Invoke-WebRequest to download files that require authentication. Simply include the username and password in the command using the -Credential parameter. For example:

$credential = Get-Credential Invoke-WebRequest -Uri https://example.com/securefile.zip -OutFile C:\Downloads\securefile.zip -Credential $credential
Get Credential Invoke WebRequest

In addition to downloading files, Invoke-WebRequest can also be used to interact with web services, submit form data, and more. So if you’re looking for a versatile tool for interacting with the web in PowerShell, be sure to check out Invoke-WebRequest.

Finding Parsing Errors

When using the Invoke-WebRequest cmdlet in PowerShell to interact with websites or web services, it’s important to be aware of parsing errors that may occur. Parsing errors can happen when the response from the website or service is not in the expected format or when there are syntax issues with the response.

To prevent parsing errors, you can use the -UseBasicParsing parameter when invoking Invoke-WebRequest. This parameter tells PowerShell to use a simpler parsing engine that is less likely to encounter errors. However, this may also mean that some content is not parsed correctly.

try {

    $response = Invoke-WebRequest -Uri https://example.com -UseBasicParsing

    # Process response

} catch {

    Write-Error "An error occurred: $($_.Exception.Message)"

}
Finding Parsing Errors

Another way to avoid parsing errors is to use try-catch blocks in your code. This allows you to catch any errors that may occur and handle them appropriately. For example, you can log the error or display a friendly message to the user.

try {

    $response = Invoke-WebRequest -Uri https://example.com

    # Process response

} catch [System.Net.WebException] {

    Write-Error "A web exception occurred: $($_.Exception.Message)"

} catch {

    Write-Error "An error occurred: $($_.Exception.Message)"

}
Find Parsing Errors

If parsing errors do occur, you can inspect the StatusCode property of the response object to see if it is not 200 (OK). If the StatusCode is not 200, then there may be an issue with the response.

Additionally, you can inspect the Content property of the response object to see the actual content returned by the website or service.

$response = Invoke-WebRequest -Uri https://example.com

if ($response.StatusCode -eq 200) {

    # Process response

} else {

    Write-Error "An error occurred: $($response.StatusCode) $($response.StatusDescription)"

}

Invoke-RestMethod

In PowerShell, you can use the Invoke-RestMethod cmdlet to download files from the internet. This cmdlet is similar to the Invoke-WebRequest cmdlet, but it is specifically designed for working with RESTful web services.

To download a file using Invoke-RestMethod, you can specify the URI of the file in the -Uri parameter and the local path where you want to save the file in the -OutFile parameter. For example:

Invoke-RestMethod -Uri https://example.com/file.zip -OutFile C:\Downloads\file.zip

This will download the file located at https://example.com/file.zip and save it to the local path C:\Downloads\file.zip.

By default, Invoke-RestMethod will use the HTTP GET method to retrieve the file. If the file requires authentication, you can specify the username and password in the –Credential parameter.

You can also use the -Headers parameter to specify custom headers to include in the request. This can be useful if the web service requires specific headers to be included in the request.

Overall, using Invoke-RestMethod to download files from the internet is a simple and convenient way to automate the downloading of files in PowerShell scripts.

Start-BitsTransfer

Downloading one File

In PowerShell, you can use the Start-BitsTransfer cmdlet to download a file from the internet. BITS (Background Intelligent Transfer Service) is a Windows service that can transfer files between machines in the background, even if the network connection is unreliable.

To download a file using Start-BitsTransfer, you need to specify the source URL of the file and the local path where you want to save the file. For example:

Start-BitsTransfer -Source https://example.com/file.zip -Destination C:\Downloads\file.zip

This will download the file located at https://example.com/file.zip and save it to the local path C:\Downloads\file.zip.

You can also use the -DisplayName parameter to specify a name for the transfer job. This can be useful if you want to monitor the progress of the download.

Start-BitsTransfer -Source https://example.com/file.zip -Destination C:\Downloads\file.zip -DisplayName "Downloading file.zip"

Additionally, you can use the -Priority parameter to specify the priority of the transfer job, and the -ProxyServer parameter to specify a proxy server to use for the transfer.

Overall, using Start-BitsTransfer is a convenient way to download files from the internet in PowerShell, especially for large files or unreliable network connections.

Downloading several Files

In PowerShell, you can use the Start-BitsTransfer cmdlet to download multiple files from the internet. BITS (Background Intelligent Transfer Service) is a Windows service that can transfer files between machines in the background, even if the network connection is unreliable.

To download multiple files using Start-BitsTransfer, you can create an array of objects that specify the source URLs and local paths for each file, and then use a foreach loop to iterate through the array and start the transfer job for each file. For example:

$fileList = @( @{ Source = "https://example.com/file1.zip"; Destination = "C:\Downloads\file1.zip" }, @{ Source = "https://example.com/file2.zip"; Destination = "C:\Downloads\file2.zip" }, @{ Source = "https://example.com/file3.zip"; Destination = "C:\Downloads\file3.zip" } ) foreach ($file in $fileList) { Start-BitsTransfer -Source $file.Source -Destination $file.Destination }
Downloading several Files

This will download the files located at https://example.com/file1.zip, https://example.com/file2.zip, and https://example.com/file3.zip, and save them to the local paths C:\Downloads\file1.zip, C:\Downloads\file2.zip, and C:\Downloads\file3.zip, respectively.

You can also use the -DisplayName parameter to specify a name for each transfer job, and the -Priority parameter to specify the priority of each transfer job.

$fileList = @( @{ Source = "https://example.com/file1.zip"; Destination = "C:\Downloads\file1.zip"; DisplayName = "Downloading file1.zip" }, @{ Source = "https://example.com/file2.zip"; Destination = "C:\Downloads\file2.zip"; DisplayName = "Downloading file2.zip" }, @{ Source = "https://example.com/file3.zip"; Destination = "C:\Downloads\file3.zip"; DisplayName = "Downloading file3.zip" } ) foreach ($file in $fileList) { Start-BitsTransfer -Source $file.Source -Destination $file.Destination -DisplayName $file.DisplayName }

Overall, using Start-BitsTransfer to download multiple files from the internet in PowerShell can save time and effort, especially when you need to download a large number of files.

WebClient Class and HttpClient Class (.NET Framework)

System.Net.WebClient

In PowerShell, you can also use the .NET class WebClient to download files from the internet. The WebClient class provides a simple interface for downloading files over HTTP or FTP protocols.

To download a file using WebClient, you need to create an instance of the class, specify the source URL of the file, and the local path where you want to save the file. For example:

$webClient = New-Object System.Net.WebClient $webClient.DownloadFile("https://example.com/file.zip", "C:\Downloads\file.zip")

This will download the file located at https://example.com/file.zip and save it to the local path C:\Downloads\file.zip.

You can also use the -Headers parameter to specify custom HTTP headers for the request, and the -Proxy parameter to specify a proxy server to use for the download.

$webClient = New-Object System.Net.WebClient $headers = @{ "Authorization" = "Bearer <token>" } $webClient.Headers.Add($headers) $webClient.Proxy = [System.Net.WebRequest]::GetSystemWebProxy() $webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials $webClient.DownloadFile("https://example.com/file.zip", "C:\Downloads\file.zip")
image 1 24

This will download the file located at https://example.com/file.zip with custom HTTP headers and use the system proxy server.

Overall, using WebClient to download files in PowerShell can be a simple and flexible alternative to other methods, especially when you need more control over the download process.

System.Net.Http.HttpClient

In PowerShell, you can also use the .NET class HttpClient from the System.Net.Http namespace to download files from the internet. HttpClient provides a more modern and flexible approach to making HTTP requests compared to WebClient.

To download a file using HttpClient, you need to create an instance of the class, create a GET request for the file, and then use the SendAsync method to send the request and receive the response. Finally, you can save the response content to a local file. For example:

$httpClient = New-Object System.Net.Http.HttpClient $response = $httpClient.GetAsync("https://example.com/file.zip").Result $content = $response.Content.ReadAsByteArrayAsync().Result [System.IO.File]::WriteAllBytes("C:\Downloads\file.zip", $content)
System.Net.Http.HttpClient

This will download the file located at https://example.com/file.zip and save it to the local path C:\Downloads\file.zip.

By now, you will have the files downloaded to your PC; To the chosen directory; Without even having to open a browser, as you do conventionally. For further queries and information, you can use the PowerShell webpage for reference. Happy Downloading!