PowerShell Logo Small

Invoke-WebRequest



This is the built-in help made by Microsoft for the command 'Invoke-WebRequest', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Gets 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>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}]
[-InformationVariable [<System.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>]



Search powershellhelp.space

DESCRIPTION


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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293988
Invoke-RestMethod
ConvertFrom-Json
ConvertTo-Json

REMARKS

<

Examples


Example 1

PS C:\>$r = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
PS C:\>$r.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -First 5
innerText---------1 =5280 feet1 mile



This command uses the Invoke-WebRequest cmdlet to send a web request to the Bing.com site.

The first command issues the request and saves the response in the $r variable.

The second command gets the InnerHtml property when it includes an equal sign, sorts the inner HTML by length and selects the 5 shortest values. Sorting by the shortest HTML
value often helps you find the most specific element that matches that text.






Example 2

The first command uses the Invoke-WebRequest cmdlet to send a sign-in request. The command specifies a value of "fb" for the value of the SessionVariable parameter, and
saves the result in the $r variable.When the command completes, the $r variable contains an HtmlWebResponseObject and the $fb variable contains a WebRequestSession object.
PS C:\>$r=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb

The second command shows the WebRequestSession object in the $fb variable.
PS C:\>$fb

The third command gets the first form in the Forms property of the HTTP response object in the $r variable, and saves it in the $form variable.
PS C:\>$form = $r.Forms[0]

The fourth command pipes the properties of the form in the $form variable into a list by using the Format-List cmdlet.
PS C:\>$form | Format-List

The fifth command displays the keys and values in the hash table (dictionary) object in the Fields property of the form.
PS C:\>$form.fields

The sixth and seventh commands populate the values of the "email" and "pass" keys of the hash table in the Fields property of the form. You can replace the email and
password with values that you want to use.
PS C:\>$form.Fields["email"]="User01@Fabrikam.com"
$form.Fields["pass"]="P@ssw0rd"

The eighth command uses the Invoke-WebRequest cmdlet to sign into the Facebook web service.The value of the Uri parameter is the value of the Action property of the form.
The WebRequestSession object in the $fb variable (the session variable specified in the first command) is now the value of the WebSession parameter. The value of the Body
parameter is the hash table in the Fields property of the form and the value of the Method parameter is POST. The command saves the output in the $r variable.
PS C:\>$r=Invoke-WebRequest -Uri ("https://www.facebook.com" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields

The full script, then, is as follows.
PS C:\># Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the results in
the $r variable.

$r=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb

# Use the session variable that you created in Example 1. Output displays values for Headers, Cookies, Credentials, etc.

$fb

# Gets the first form in the Forms property of the HTTP response object in the $r variable, and saves it in the $form variable.

$form = $r.Forms[0]

# Pipes the form properties that are stored in the $forms variable into the Format-List cmdlet, to display those properties in a list.

$form | Format-List

# Displays the keys and values in the hash table (dictionary) object in the Fields property of the form.

$form.fields

# The next two commands populate the values of the "email" and "pass" keys of the hash table in the Fields property of the form. Of course, you can replace the email and
password with values that you want to use.

$form.Fields["email"] = "User01@Fabrikam.com"
$form.Fields["pass"] = "P@ssw0rd"

# The final command uses the Invoke-WebRequest cmdlet to sign in to the Facebook web service.

$r=Invoke-WebRequest -Uri ("https://www.facebook.com" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields

When the command finishes, the StatusDescription property of the web response object in the $r variable indicates that the user is signed in successfully.
PS C:\>$r.StatusDescription



This example shows how to use the Invoke-WebRequest cmdlet with a stateful web service, such as Facebook.






Example 3

PS C:\>(Invoke-WebRequest -Uri "http://msdn.microsoft.com/en-us/library/aa973757(v=vs.85).aspx").Links.Href



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