Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Invoke-WebRequest: Perform HTTP Requests, Download Files, Parse Web with PowerShell

January 18, 2023 PowerShellWindows 10Windows Server 2019

Invoke-WebRequest: Perform HTTP Requests, Download Files, Parse Web with PowerShell

The Invoke-WebRequest cmdlet can be used to request HTTP/HTTPS/FTP resources directly from the PowerShell console. You can use this command to send HTTP requests (GET and POST), download files from a website, parse HTML web pages, perform authentication, fill out and submit web forms, etc. In this article, we’ll cover basic examples of using the Invoke-WebRequest cmdlet in PowerShell to interact with web services.

Contents:
  • Get Web Page Content with Invoke-WebRequest Cmdlet
  • Using Invoke-WebRequest with Authentication
  • Parse and Scrape HTML a Web Page with PowerShell
  • How to Download File over HTTP/FTP with PowerShell Wget (Invoke-WebRequest)?
  • How to Fill and Submit HTML Form with PowerShell?
  • Invoke-WebRequest: Ignore SSL/TLS Certificate Checks

Get Web Page Content with Invoke-WebRequest Cmdlet

The Invoke-WebRequest cmdlet allows you to send the HTTP, HTTPS, or FTP request with the GET method to the specified web page and receive a response from the server. This cmdlet has been available on Windows since version PowerShell 3.0.

There are two aliases for the Invoke-WebRequest command in Windows: iwk and wget.

Run the following command:

Invoke-WebRequest -Uri "https://woshub.com"

Invoke-WebRequest get webpage content using powershell

Tip. If you are connected to the Internet via a proxy server, you must properly configure PowerShell to access the Web through the proxy server. If you do not set proxy parameters, you will receive an error when you run the IWK command:

Invoke-WebRequest:  Unable to connect to the remote server
CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Invoke-WebRequest: Unable to connect to the remote server

The command loaded the page and displayed its contents in the PowerShell console. The returned response is not just the HTML code of the page. The Invoke-WebRequest cmdlet returns an object of type HtmlWebResponseObject. Such an object is a collection of forms, links, images, and other important elements of an HTML document. Let’s look at all the properties of this object:

$WebResponseObj = Invoke-WebRequest -Uri "https://woshub.com"
$WebResponseObj| Get-Member

HtmlWebResponseObject properties

To get the raw HTML code of the web page that is contained in the HtmlWebResponseObject object, run:

$WebResponseObj.content

You can list the HTML code along with the HTTP headers returned by the web server:

$WebResponseObj.rawcontent

Invoke-WebRequest get html raw code and http statuses on webpage

You can check only the web server HTTP status code and the HTTP headers of the HTML page:

$WebResponseObj.Headers

As you can see, the server has returned a response 200. This means that the request has been successful, and the web server is available and works correctly.

Key               Value
---               -----
Transfer-Encoding chunked
Connection        keep-alive
Vary              Accept-Encoding,Cookie
Cache-Control     max-age=3, must-revalidate
Content-Type      text/html; charset=UTF-8
Date              Wed, 13 Jul 2022 02:28:32 GMT
Server            nginx/1.20.2
X-Powered-By      PHP/5.6.40

To get the last modification time of a web page:

$WebResponseObj.ParsedHtml | Select lastModified

powershell get webpage lastmodified date

You can specify a User Agent string when connecting to a web resource. PowerShell has a set of built-in User Agent strings:

invoke-webRequest -Uri $uri -userAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::Chrome)

The list of available agents in PowerShell can be displayed like this:

[Microsoft.PowerShell.Commands.PSUserAgent].GetProperties() | Select-Object Name, @{n='UserAgent';e={ [Microsoft.PowerShell.Commands.PSUserAgent]::$($_.Name) }}

powershell: set user agent

Or you can set your own UserAgent string:

Invoke-WebRequest -Uri $uri -UserAgent 'MyApplication/1.1'

Using Invoke-WebRequest with Authentication

Some web resources require authentication to access. You can use various types of authentication with the Invoke-WebRequest cmdlet (Basic, NTLM, Kerberos, OAuth, or certificate authentication).

To perform Basic Authentication (authentication by name and password encoded in base64), you first need to get the username and password:

$cred = Get-Credential
wget -Uri 'https://somesite.com' -Credential $cred

In order to use the current Windows user credentials to perform NTLM or Kerberos authentication, add the -UseDefaultCredentials option:

Invoke-WebRequest 'https://somesite.com' -UseDefaultCredentials

DefaultCredentials not working with Basic authentication.

To authenticate with a certificate, you need to specify its thumbprint:

Invoke-WebRequest 'https://somesite.com' -CertificateThumbprint xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You can use modern Bearer/OAuth token authentication in your PowerShell scripts.

  1. First, you need to get an OAuth token from your REST API provider (out of the scope of this post);
  2. Convert token with ConvertTo-SecureString cmdlet: $Token = "12345678912345678901234567890" | ConvertTo-SecureString -AsPlainText –Force
  3. Now you can perform OAuth authentication:
    $Params = @{
    Uri = "https://somesite.com"
    Authentication = "Bearer"
    Token = $Token }
    Invoke-RestMethod @Params

Parse and Scrape HTML a Web Page with PowerShell

The Invoke-WebRequest cmdlet allows you to quickly and conveniently parse the content of any web page. When processing an HTML page, collections of links, web forms, images, scripts, etc., are created.

Let’s look at how to access specific objects on a web page. For example, I want to get a list of all outgoing links (A HREF objects) on the target HTML web page:

$SiteAdress = "https://woshub.com"
$HttpContent = Invoke-WebRequest -URI $SiteAdress
$HttpContent.Links | Foreach {$_.href }

Invoke-WebRequest create html hrefs collection

To get the link text itself (contained in the InnerText element), you can use the following command:

$HttpContent.Links | fl innerText, href

You can only select links with a specific CSS class:

$HttpContent.Links | Where-Object {$_.class -eq "page-numbers"} | fl innerText, href

Or specific text in the URL address:

$HttpContent.Links | Where-Object {$_.href -like "*powershell*"} | fl innerText,href

filtering html objects with posh

Then display a list of all images on this page:

$Img.Images

Create a collection of full URL paths to these images:

$images = $Img.Images | select src

Initialize a new instance of WebClient class:

$wc = New-Object System.Net.WebClient

And download all the image files from the page (with their original filenames) to the c:\too1s\ folder:

$images | foreach { $wc.DownloadFile( $_.src, ("c:\tools\"+[io.path]::GetFileName($_.src) ) ) }

Invoke-WebRequest parsing html page

An interesting example of using the Invoke-WebRequest cmdlet can be found in the article “Get the External IP Address Using PowerShell“.

How to Download File over HTTP/FTP with PowerShell Wget (Invoke-WebRequest)?

Invoke-WebRequest allows you to download files from a web page or FTP site (works like Wget or cURL on Windows). Suppose, you want to download a file from an HTTP using PowerShell. Run the following PowerShell command:

wget "https://download-installer.cdn.mozilla.net/pub/firefox/releases/102.0.1/win64/en-US/Firefox%20Setup%20102.0.1.exe" -outfile “c:\tools\firefox_setup.exe”

powershell: using wget alias to download http/https file

This command will download the file from the HTTP site and save it to the specified directory.

You can also download files from a Windows web server using BITS in synchronous mode.

You can get the size of a file in MB before downloading it with wget:

$url = "https://download-installer.cdn.mozilla.net/pub/firefox/releases/102.0.1/win64/en-US/Firefox%20Setup%20102.0.1.exe"
(Invoke-WebRequest $url -Method Head).Headers.'Content-Length'/1Mb

wget: powershell get file size by http link

Below is an example of a PowerShell script that will find all links to *.pdf files on a target web page and bulk download all found files from the website to your computer (each pdf file is saved under a random name):

$OutDir="C:\docs\download\PDF"
$SiteAdress = "https://sometechdocs.com/pdf"
$HttpContent = Invoke-WebRequest -URI $SiteAdress
$HttpContent.Links | Where-Object {$_.href -like "*.pdf"} | %{Invoke-WebRequest -Uri $_.href -OutFile ($OutDir + $(Get-Random 200000)+".pdf")}

As a result of the script in the target directory, all pdf files from the page will be downloaded. Each file is saved under a random name.

In modern PowerShell Core (6.1 and newer), the Invoke-WebRequest cmdlet supports resume mode. Update your version of PowerShell Core and you can use the -Resume option on the Invoke-WebRequest command to automatically resume downloading a file in case of a communication channel or server is unavailable:

Invoke-WebRequest -Uri $Uri -OutFile $OutFile –Resume

How to Fill and Submit HTML Form with PowerShell?

Many web services require filling various data into HTML forms. With Invoke-WebRequest, you can access any HTML form, fill in the necessary fields, and submit the completed form back to the server. In this example, we’ll show how to sign in to Facebook through its standard web form using PowerShell.

Fill and send Facebook login form with Powershell

With the following command, we will save the information about connection cookies in a separate session variable:

$fbauth = Invoke-WebRequest https://www.facebook.com/login.php -SessionVariable session

Using the next command, display the list of the fields to fill in the login HTML form (login_form):

$fbauth.Forms["login_form"].Fields

Assign the desired values to all fields:

$fbauth.Forms["login_form"].Fields["email"] = "[email protected]"

$fbauth.Forms["login_form"].Fields["pass"] = "Coo1P$wd"

Etc.

To submit (sent) the filled form to the server, call the action attribute of the HTML form:

$Log = Invoke-WebRequest -method POST -URI ("https://www.facebook.com/login.php" + $fbauth.Forms["login_form"].Action) -Body $fbauth.Forms["login_form"].Fields -WebSession $session

You can also use the JSON format to send data to a web page with the POST method:

$headers = @{
'Content-Type'='application/json'
'apikey'='0987654321'
}
$jsonbody = @{
"siteUrl" ="https://somesite.com"
"email" = "[email protected]"
}
Invoke-WebRequest -Method 'Post' -Uri $url -Body ($jsonbody |ConvertTo-Json) -Headers $headers -ContentType "application/json"

Invoke-WebRequest: Ignore SSL/TLS Certificate Checks

Another issue is that the Invoke-WebRequest cmdlet is closely related to Internet Explorer. For example, in Windows Server Core editions in which IE is not installed (or removed), the Invoke-WebRequest cmdlet cannot be used.

Invoke-WebRequest: The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer’s first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

In this case, the WebClient class can be used instead of Invoke-WebRequest. For example, to download a file from a specified URL, use the command.

(New-Object -TypeName 'System.Net.WebClient').DownloadFile($Url, $FileName)

If an invalid SSL certificate is used on an HTTPS site, or PowerShell doesn’t support this type of SSL/TLS protocol, then the Invoke-WebRequest cmdlet drops the connection.

Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.

invoke-webrequest powershell: couldnt establish trust relationship SSL TLS secure channel

By default, Windows PowerShell (in early builds of Windows 10, Windows Server 2016, and older versions of Windows) uses the legacy and insecure TLS 1.0 protocol for connections (check the blog post describing the PowerShell module installation error: Install-Module: Unable to download from URI).

If the legacy TLS 1.0 and TLS 1.1 protocols are not disabled in Windows, you must run the following command in order to use TLS 1.2 in PowerShell connections:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

If a self-signed certificate is used on the HTTPS site, the Invoke-WebRequest cmdlet refuses to receive data from it. To ignore (skip) an invalid SSL certificate, use the following PowerShell code:

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$result = Invoke-WebRequest -Uri "https://somesite.com"

In PowerShell Core, the Invoke-WebRequest cmdlet has an additional parameter –SkipCertificateCheck that allows you to ignore invalid SSL/TLS certificates.

Another significant drawback of the Invoke-WebRequest cmdlet is its rather low performance. When downloading a file, the HTTP stream is entirely buffered into memory, and only after the full complete download is completed is saved to disk. Thus, when downloading large files using Invoke-WebReques, you may run out of RAM.

9 comments
3
Facebook Twitter Google + Pinterest
previous post
Configuring Port Forwarding in Windows
next post
Windows Setup Couldn’t Create a New Partition

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

9 comments

Sakshi February 4, 2016 - 5:04 am

Hi,
There is an existing login form.I want to enter data in the text field of the form.
The name of the textbox is “username”.How can i enter data using powershell scripting?

Reply
Ron December 4, 2016 - 8:21 pm

Anyone try this lately?
Invoke-WebRequest : The request was aborted: The connection was closed unexpectedly.

Reply
admin December 7, 2016 - 6:41 am

Try to run the script under Elevated session that has Administrator privileges

Reply
bege May 16, 2018 - 8:13 pm

How can I filter this output “$HttpContent.Links | fl href” for those links in your example that have innerText containing “20” to get the url of a certain link?

Reply
Rupi February 18, 2019 - 6:00 pm

I only want to download a PDF-file by PowerShell from a given URL when it is updated (PDF file-size is ~ 100MB).
Filename and path stay unchanged (remote and local)
A PDF download is easily done with below command, but if this is executed by the Windows Task Scheduler it downloads and overwrite the local existing PDF each time it is triggered by the Scheduler.

Invoke-WebRequest -Uri “https://my.site.com/download/path/MyMonitoredPdf.pdf”

I only want a download if remote and local files are different.
wget seems to have some options for downloading only newer files (wget -N, –timestamping)
In case a file timestamp is available only newer, updated files are downloaded.

How do I do this in PowerShell?

Reply
manoj May 30, 2019 - 1:06 pm

Hi, thanks for the post, I got some knowledge, However my task is still incomplete

Reply
Tugay November 24, 2021 - 1:28 pm

Hello,

if I try to call command ‘$fbauth.Forms[“login_form”].Fields’, I’m getting the error:
‘Cannot index into a null array’

Can you help me?

Reply
BennyBeat December 29, 2021 - 4:13 pm

Hi,
In order to create an unattended process after install the OS, I’m trying this method to get the Java URL offline installers from “https://www.java.com/en/download/manual.jsp” since they change from time to time, but I’m stuck on bypass the Cookie pop-up and getting the URL files.
I successfully did that operation of Unattended Download and Post-install programs (7-zip, Java, LibreOffice, Mozilla Firefox…via Scripts Folder in Windows ISO and adding some config or .cmd files in .Wim) not only getting the right URL files, but also bypass the Cookie message via Aria and cURL/Wget. Unfortunately, even these programs are reputed FOSS, in some environments I can’t use 3rd party apps to do that. Perhaps this PowerShell method can’t work with “.jsp” pages? Thank you in advance!
Benny.

Reply
MyName July 14, 2022 - 1:25 pm

$fbauth.Forms[“login_form”].Fields[“email”] = “[email protected]”
$fbauth.Forms[“login_form”].Fields[“pass”] = “Coo1P$wd”
Cannot index into a null array.
At line:2 char:1
+ $fbauth.Forms[“login_form”].Fields[“email”] = “[email protected]”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At line:3 char:1
+ $fbauth.Forms[“login_form”].Fields[“pass”] = “Coo1P$wd”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Delete Old User Profiles in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top