The “wget” is a command line utility used for retrieving files via the most extensively used Internet protocols such HTTP, FTP, HTTPS, and FTPS. Because it is a non-interactive command line tool, it can be invoked from scripts, terminals, etc. Do you want to download files from the internet yet don’t like the idea of clicking links over and over? You can automate this process by using PowerShell.

Windows PowerShell is an automation and scripting platform. It is primarily intended for system administrators and IT professionals to handle and automate the processes related to Windows administration and its applications.

PowerShell comprises various useful functions and commands are available in PowerShell, which are called cmdlets. The Microsoft version of wget is available as a fundamental command in PowerShell (PS) 3.0 known as Invoke-WebRequest. The wget exists as an alias in the Invoke-WebRequest command.

Invoke-WebRequest Command in PowerShell

Invoke-WebRequest is a non-interactive network downloader, or we can say that it is a command that permits a system to download files from any website in the background without requiring a user to log in. HTTP and HTTPS requests are sent to the specific web service or page.

After that, this command parses the request-response and returns collections of important HTML components such as images, links, etc. The Invoke-WebRequest command can also control requests with credentials regardless of whether the source location requires the user to be logged in.

Write out the below-given command in your PowerShell to have a better understanding of Invoke-Web Request, its syntax, and aliases:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/1-5.jpg" data-lazy- height="542" src="data:image/svg xml,” width=”934″>

A Simple File Request with PowerShell

In this section, we will try to execute the Invoke-WebRequest for file requests. For that, we will specify the URI in the following command with the “-UseBasicParsing” option. This option is used for backward compatibility. For instance, in a case where Internet Explorer is not installed or configured.

> Invoke-WebRequest http://httpbin.org/json -UseBasicParsing

The Invoke-WebRequest cmdlet will return an Html Web Response Object that has a lot of helpful information about the HTML parsing properties such as raw content, headers, links, forms, images, and input fields, etc.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/2-5.jpg" data-lazy- height="658" src="data:image/svg xml,” width=”929″>

Admins can use the Invoke-WebRequest command to transport data over networks and test services via the internet. Whereas wget was created with varied network conditions, making it excellent for unreliable connections and slow systems. In PowerShell, the “wget” command can be used to extract an HTML Web Object, such as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/3-5.jpg" data-lazy- height="537" src="data:image/svg xml,” width=”907″>

Other than this, any different wget command execution will show you the following error in your PowerShell:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/4-5.jpg" data-lazy- height="334" src="data:image/svg xml,” width=”928″>

So, it’s better to utilize “Invoke-WebRequest” for the same purpose, which uses “wget” as an alias.

Extracting Links in PowerShell

You can also utilize Invoke-WebRequest for extracting a list of links that exist on a particular web file. “-Uri” option is used to state the Uniform Resource Identifier (URI) of your internet resource. Now, check the command given below:

> (Invoke-WebRequest -Uri “https://aka.ms/pscore6-docs” -UseBasicParsing

).Links.Href

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/5-5.jpg" data-lazy- height="563" src="data:image/svg xml,” width=”961″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/6-5.jpg" data-lazy- height="592" src="data:image/svg xml,” width=”977″>

Download Any File from the Website with PowerShell

We have seen the method of requesting a file and extracting links from the web using Invoke-WebRequest. Now, we will move forward to download a complete file from the specified URI. We will execute a PowerShell script for this purpose. So, open up your Windows PowerShell ISE and create a new file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/7-5.jpg" data-lazy- height="639" src="data:image/svg xml,” width=”788″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/8-4.jpg" data-lazy- height="595" src="data:image/svg xml,” width=”910″>

After that, write out the below-given code in your PowerShell Script. Specify the URI in the “$source” variable. “$destination” indicates the path where this file will be placed after downloading. “-Outfile” option is used to specify the output file on which the Invoke-WebRequest command will save its response.

$source = ‘http://speedtest.tele2.net/10MB.zip’

$destination = ‘E:download10MB.zip’


Invoke-WebRequest -Uri $source -OutFile $destination

Save the file as “testfile1.ps1”, and run this PowerShell script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/9-5.jpg" data-lazy- height="215" src="data:image/svg xml,” width=”833″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/10-6.jpg61133b40ddde3.jpg" data-lazy- height="389" src="data:image/svg xml,” width=”918″>

The Error-free output declares that the file is successfully downloaded. To confirm the file presence, use the “Get-ChildItem” cmdlet for viewing the content of the specified folder:

> Get-ChildItem “E:download”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/11-5.jpg61133b40e4037.jpg" data-lazy- height="349" src="data:image/svg xml,” width=”927″>

Sending Web Requests with PowerShell

Now, we will write a script to send a web request to the website “bing.com.” Invoke-WebRequest command will issue the request to the specified site. After that, the website response will be saved on the “$Response” variable.

The next command will extract the input filed values and pipe them to the Select-Object using the pipe operator [“|”].

$Response = Invoke-WebRequest -URI https://www.bing.com/search?q=how many feet in a mile

$Response.InputFields | Where-Object {


    $_.name -like “* Value*”

} | Select-Object Name, Value

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/12-3.jpg" data-lazy- height="229" src="data:image/svg xml,” width=”908″>

Execution of the “testfile2.ps1” will show you the following output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/13-3.jpg" data-lazy- height="230" src="data:image/svg xml,” width=”692″>

If you get the error shown in the below-given output, use the “-UseBasicParsing” option after specifying the URI in the Invoke-WebRequest command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/14-3.jpg" data-lazy- height="298" src="data:image/svg xml,” width=”877″>

$Response = Invoke-WebRequest -URI https://www.bing.com/search?q=how many feet in a mile -UseBasicParsing

$Response.InputFields | Where-Object {


    $_.name -like “* Value*”

} | Select-Object Name, Value

Now, execute the same script with the mentioned changes, and you will get the error-free output.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/15-3.jpg61133b4107283.jpg" data-lazy- height="274" src="data:image/svg xml,” width=”1213″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/16-2.jpg" data-lazy- height="230" src="data:image/svg xml,” width=”692″>

Conclusion

Most of the users get tired downloading the files by clicking over and over again. This process also requires your engagement and precious time. You can now use PowerShell to automate this download procedure which assists you to be more productive and lets you engage in other activities when your files are downloading.

PowerShell provides several useful commands known as cmdlets; Invoke-WebRequest is one of them. You can run wget from PowerShell, which is an alias for the Invoke-WebRequest command. This article showed you the methods for requesting, sending, and downloading a web file, extracting links from the web.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/talha-150×150.jpg61133b4113311.jpg" height="112" src="data:image/svg xml,” width=”112″>

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.