Invoke-WebRequest

Get content from a web page on the Internet.

Syntax
      Invoke-WebRequest [-Uri] Uri [-Body Object] [-Certificate X509Certificate] [-CertificateThumbprint String] 
         [-ContentType String] [-Credential PSCredential] [-DisableKeepAlive] [-Headers IDictionary]
            [-InFile String] [-MaximumRedirection Int32]
               [-Method {Default | Get | Head | Post | Put | Delete | Trace | Options | Merge | Patch}]
                  [-OutFile String] [-PassThru] [-Proxy Uri] [-ProxyCredential PSCredential] 
                     [-ProxyUseDefaultCredentials] [-SessionVariable String] [-TimeoutSec Int32]
                        [-TransferEncoding {chunked | compress | deflate | gzip | identity}]
                           [-UseBasicParsing] [-UseDefaultCredentials] [-UserAgent String] 
                              [-WebSession WebRequestSession] [CommonParameters]

Key
   -Body Object
       The body of the request. The body is the content of the request that follows the headers.
       You can also pipe a body value to Invoke-WebRequest.

       The Body parameter can be used to specify a list of query parameters or specify the content of the response.

       When the input is a GET request and the body is an IDictionary (typically, a hash table), the body is added to 
       the URI as query parameters. For other GET requests, the body is set as the value of the request body in the 
       standard name=value format.

       When the body is a form, or it is the output of an Invoke-WebRequest call, Windows PowerShell sets the request 
       content to the form fields.

       For example:
        
       `$R = Invoke-WebRequest http://website.com/login.aspx` `$R.Forms[0].Name = "MyName"` `$R.Forms[0].Password = 
       "MyPassword"` `Invoke-RestMethod http://website.com/service.aspx -Body $R`

       - or -

       `Invoke-RestMethod http://website.com/service.aspx -Body $R.Forms[0]`

   -Certificate X509Certificate
       The client certificate that is used for a secure web request. Enter a variable that contains a certificate
       or a command or expression that gets the certificate.

       To find a certificate, use Get-PfxCertificate or use the Get-ChildItem cmdlet in the Certificate (Cert:) 
       drive. If the certificate is not valid or does not have sufficient authority, the command fails.

   -CertificateThumbprint String
       The digital public key certificate (X509) of a user account that has permission to send the request. 
       Enter the certificate thumbprint of the certificate.

       Certificates are used in client certificate-based authentication. They can be mapped only to local user 
       accounts; they do not work with domain accounts.

       To get a certificate thumbprint, use the Get-Item or Get-ChildItem command in the PowerShell Cert: drive.

   -ContentType String
       The content type of the web request.

       If this parameter is omitted and the request method is POST, Invoke-WebRequest sets the content type to 
       application/x-www-form-urlencoded. Otherwise, the content type is not specified in the call.

   -Credential PSCredential
       A user account that has permission to send the request. The default is the current user.

       Type a user name, such as User64 or ss64Dom\User64, or enter a PSCredential object, such as one generated by 
       the Get-Credential cmdlet.

   -DisableKeepAlive
       Indicates that the cmdlet sets the KeepAlive value in the HTTP header to False. By default, KeepAlive is True. 
       KeepAlive establishes a persistent connection to the server to facilitate subsequent requests.

   -Headers IDictionary
       The headers of the web request. Enter a hash table or dictionary.

       To set UserAgent headers, use the UserAgent parameter. You cannot use this parameter to specify UserAgent or 
       cookie headers.

   -InFile String
       Gets the content of the web request from a file.

       Enter a path and file name. If you omit the path, the default is the current location.

   -MaximumRedirection Int32
       Specifies how many times Windows PowerShell redirects a connection to an alternate Uniform Resource Identifier 
       (URI) before the connection fails. The default value is 5. A value of 0 (zero) prevents all redirection.

   -Method WebRequestMethod
       The method used for the web request. The acceptable values for this parameter are:

        - Default
        - Delete
        - Get
        - Head
        - Merge
        - Options
        - Patch
        - Post
        - Put
        - Trace

   -OutFile String
       The output file for which this cmdlet saves the response body. Enter a path and file name.
       If you omit the path, the default is the current location.

       By default, Invoke-WebRequest returns the results to the pipeline. To send the results to a file and to the 
       pipeline, use the Passthru parameter.

   -PassThru
       Indicates that the cmdlet returns the results, in addition to writing them to a file. This parameter is valid 
       only when the OutFile parameter is also used in the command.

   -Proxy Uri
       A proxy server for the request, rather than connecting directly to the Internet resource. Enter the 
       URI of a network proxy server.

   -ProxyCredential PSCredential
       A user account that has permission to use the proxy server that is specified by the -Proxy parameter. 
       The default is the current user.

       Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated 
       by the Get-Credential cmdlet.

       This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the 
       ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.

   -ProxyUseDefaultCredentials
       Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is 
       specified by the Proxy parameter.

       This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the 
       ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.

   -SessionVariable String
       Specifies a variable for which this cmdlet creates a web request session and saves it in the value. Enter a 
       variable name without the dollar sign ($) symbol.

       When you specify a session variable, Invoke-WebRequest creates a web request session object and assigns it to 
       a variable with the specified name in your Windows PowerShell session. You can use the variable in your 
       session as soon as the command completes.

       Unlike a remote session, the web request session is not a persistent connection. It is an object that contains 
       information about the connection and the request, including cookies, credentials, the maximum redirection 
       value, and the user agent string. You can use it to share state and data among web requests.

       To use the web request session in subsequent web requests, specify the session variable in the value of the 
       WebSession parameter. Windows PowerShell uses the data in the web request session object when establishing the 
       new connection. To override a value in the web request session, use a cmdlet parameter, such as UserAgent or 
       Credential . Parameter values take precedence over values in the web request session.
       You cannot use the SessionVariable and WebSession parameters in the same command.

   -TimeoutSec Int32
       How long the request can be pending before it times out. Enter a value in seconds. The default 
       value, 0, specifies an indefinite time-out.

       A Domain Name System (DNS) query can take up to 15 seconds to return or time out. If your request contains a 
       host name that requires resolution, and you set TimeoutSec to a value greater than zero, but less than 15 
       seconds, it can take 15 seconds or more before a WebException is thrown, and your request times out.

   -TransferEncoding String
       A value for the transfer-encoding HTTP response header. The acceptable values for this parameter are:
        - Chunked
        - Compress
        - Deflate
        - GZip
        - Identity

   -Uri Uri
       The Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent. 
       Enter a URI. This parameter supports HTTP, HTTPS, FTP, and FILE values.

       This parameter is required. The parameter name ( Uri ) is optional.

   -UseBasicParsing
       This parameter has been deprecated. Beginning with PowerShell 6.0.0, all Web requests use basic parsing only.
       This parameter is included for backwards compatibility only and any use of it has no effect on the operation
       of the cmdlet.
       Indicates that the cmdlet uses the response object for HTML content without using the Internet Explorer
       Document Object Model (DOM) parsing.

   -UseDefaultCredentials
       Indicates that the cmdet uses the credentials of the current user to send the web request.
       Use this when downloading files from SharePoint.

   -UserAgent String
       A user agent string for the web request.

       The default user agent is similar to Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) WindowsPowerShell/3.0 
       with slight variations for each Operating System and platform.

       To test a website with the standard user agent string that is used by most Internet browsers, use the 
       properties of the PSUserAgent class, such as Chrome, FireFox, InternetExplorer, Opera, and Safari.

       For example, the following command uses the user agent string for Internet Explorer:

       `Invoke-WebRequest -Uri http://website.com/ -UserAgent 
       ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer)`

   -WebSession WebRequestSession
       A web request session. Enter the variable name, including the dollar sign ($).

       To override a value in the web request session, use a cmdlet parameter, such as UserAgent or Credential . 
       Parameter values take precedence over values in the web request session.

       Unlike a remote session, the web request session is not a persistent connection. It is an object that contains 
       information about the connection and the request, including cookies, credentials, the maximum redirection 
       value, and the user agent string. You can use it to share state and data among web requests.

       To create a web request session, enter a variable name (without a dollar sign) in the value of the 
       SessionVariable parameter of an Invoke-WebRequest command. Invoke-WebRequest creates the session and saves it 
       in the variable. In subsequent commands, use the variable as the value of the WebSession parameter.

       You cannot use the SessionVariable and WebSession parameters in the same command.

The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements. This cmdlet was introduced in Windows PowerShell 3.0.

To get 10x faster download with Invoke-WebRequest, set $ProgressPreference = 'SilentlyContinue'
In PowerShell Core6 this is not needed. [via @CPoweredLion]

Standard Aliases for Invoke-WebRequest: IWR (prior to PS 7.0 there was wget and curl now replaced by curl).

Examples

Retrieve the Google status page and save to a static .html file locally:

PS C:\> $Req = Invoke-WebRequest -URI "https://status.cloud.google.com/" | Select-Object -Expand Content

PS C:\> $Req > GoogleStatus.html

Get all the hyperlinks from a web page:

PS C:\> (Invoke-WebRequest -Uri 'https://ss64.com/nt/').Links.Href

This command gets the links in a web page. It uses Invoke-WebRequest to get the page content. Then it uses the .Links property of the HtmlWebResponseObject that Invoke-WebRequest returns, and returns the .Href property of each link.

Download an image from a website:

$url = 'https://ss64.com/images/smile.jpg'
Invoke-WebRequest -Uri $url -OutFile "$env:temp\ss64-smile.jpg"

Retrieve the HTTP header from a website:

$url = 'https://ss64.com/'
(Invoke-WebRequest -Method Head -Uri $url).RawContent

Retrieve all the links from a web page, removing any duplicates:

(Invoke-WebRequest -Uri 'https://ss64.com/ps/').Links.Href | Select-Object -Unique

Download the latest version of Firefox for Windows (English-US) and perform a silent Install:

$TempInstall = "$env:temp\firefox_setup.exe"
$url = 'https://download.mozilla.org/?product=firefox-latest&os=win&lang=en-US'
Invoke-WebRequest -Uri $url -OutFile $TempInstall
Start-Process -FilePath $TempInstall -Args '/DesktopShortcut=false' -Wait
Remove-Item -Path $TempInstall

Retrieve a specific command page from ss64.com/ps, and then display the quotation by selecting the class 'quote':

function ss64Quotes {
param(
[string]$command
)
$command = $command.ToLower()
$geturl = Invoke-WebRequest -uri ss64.com/ps/$command.html

$quote = $geturl.AllElements | ? { $_.Class -eq 'quote' } | Select-Object innertext

Write-Host $quote.innerText
Get-Help $command
}

(via Reddit/PowerShell)

“I think that I've learned to relax, and trust in and hire very talented people, and trust in their abilities a little more” ~ Mark Romanek

Related PowerShell Cmdlets

ConvertTo-Json - Convert an object to a JSON-formatted string.
Invoke-RestMethod - Send an HTTP or HTTPS request to a RESTful web service.
New-WebServiceProxy - Create a Web service proxy object.
How-To: Retrieve UK Bank Holidays using PowerShell


 
Copyright © 1999-2024 SS64.com
Some rights reserved