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:
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.
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.
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:
Other than this, any different wget command execution will show you the following error in your PowerShell:
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:
).Links.Href
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.
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.
$destination = 'E:\download\10MB.zip'
Invoke-WebRequest -Uri $source -OutFile $destination
Save the file as “testfile1.ps1”, and run this PowerShell script.
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:
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.InputFields | Where-Object {
$_.name -like "* Value*"
} | Select-Object Name, Value
Execution of the “testfile2.ps1” will show you the following output:
If you get the error shown in the below-given output, use the “-UseBasicParsing” option after specifying the URI in the Invoke-WebRequest command.
$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.
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.