Invoke-WebRequest
Published: February 29, 2012
Updated: August 15, 2012
Applies To: Windows PowerShell 3.0
Invoke-WebRequest
Syntax
Parameter Set: Default Invoke-WebRequest [-Uri] <Uri> [-Body <Object> ] [-Certificate <X509Certificate> ] [-CertificateThumbprint <String> ] [-ContentType <String> ] [-Credential <PSCredential> ] [-DisableKeepAlive] [-Headers <IDictionary> ] [-InFile <String> ] [-MaximumRedirection <Int32> ] [-Method <WebRequestMethod> ] [-OutFile <String> ] [-PassThru] [-Proxy <Uri> ] [-ProxyCredential <PSCredential> ] [-ProxyUseDefaultCredentials] [-SessionVariable <String> ] [-TimeoutSec <Int32> ] [-TransferEncoding <String> ] [-UseBasicParsing] [-UseDefaultCredentials] [-UserAgent <String> ] [-WebSession <WebRequestSession> ] [ <CommonParameters>]
Detailed Description
The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web service. It also parses the response, exposing collections of forms, links, images, and other significant HTML elements.
This parameter is introduced in Windows PowerShell 3.0.
Parameters
-Body<Object>
Specifies the body of the request. The body is the content of the request that follows the headers.
You can also pipe a request body to Invoke-WebRequest.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
True (ByValue) |
|
Accept Wildcard Characters? |
false |
-Certificate<X509Certificate>
Specifies 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-CertificateThumbprint<String>
Specifies 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 Windows PowerShell Cert: drive.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-ContentType<String>
Specifies the content type of the web request, such as "application/x-www-form-urlencoded".
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Credential<PSCredential>
Specifies a user account that has permission to send the request. 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
Current user |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-DisableKeepAlive
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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
KeepAlive |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Headers<IDictionary>
Specifies the headers for the web request. Enter a hash table or dictionary.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-InFile<String>
Specifies the path to the file that contains the web request.
Enter a path and file name. If you omit the path, the default is the current location.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-MaximumRedirection<Int32>
Determines 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
5 |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Method<WebRequestMethod>
Specifies the method used for the web request. Valid values are Default, Delete, Get, Head, Options, Post, Put, and Trace.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
Default |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-OutFile<String>
Sends the results to the specified output file. 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-PassThru
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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Proxy<Uri>
Uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-ProxyCredential<PSCredential>
Specifies 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
Current user |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-ProxyUseDefaultCredentials
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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-SessionVariable<String>
Specifies a name for the session variable. Enter a variable name without the dollar sign ($) symbol.
When you use the session variable in a web request, the variable is populated with a WebRequestSession object.
You cannot use the SessionVariable and WebSession parameters in the same command.
|
Aliases |
SV |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-TimeoutSec<Int32>
Specifies 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.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
0 |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-TransferEncoding<String>
Specifies a value for the transfer-encoding HTTP response header. Valid values are Chunked, Compress, Deflate, GZip and Identity.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Uri<Uri>
Specifies the Uniform Resource Identifier (URI) of the Internet resource to which the web request is sent.
This parameter is required. The parameter name (-Uri) is optional.
|
Aliases |
none |
|
Required? |
true |
|
Position? |
1 |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-UseBasicParsing
Uses the response object for HTML content without Document Object Model (DOM) parsing.
This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-UseDefaultCredentials
Uses the credentials of the current user to send the web request.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-UserAgent<String>
Specifies a user agent string for the web request.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-WebSession<WebRequestSession>
Specifies a web request session to store data for subsequent requests.
You cannot use the SessionVariable and WebSession parameters in the same command.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Inputs
The input type is the type of the objects that you can pipe to the cmdlet.
-
System.Object
You can pipe the body of a web request to Invoke-WebRequest
Outputs
The output type is the type of the objects that the cmdlet emits.
- Microsoft.PowerShell.Commands.HtmlWebResponseObject
Examples
Example 1
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.
PS C:\> $r = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+milePS C:\>$r.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -First 5
innerText---------1 =5280 feet1 mile
Example 2
This example shows how to use the Invoke-WebRequest cmdlet with a stateful web service, such as Facebook.
The first command uses the Invoke-WebRequest cmdlet to send a login 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:\> $fbHeaders : {}Cookies : System.Net.CookieContainerUseDefaultCredentials : FalseCredentials :Certificates :UserAgent : Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) WindowsPowerShell/3.0Proxy :MaximumRedirection : -1
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.
The fourth command uses the Format-List cmdlet to display the properties of the form in the $form variable in a 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 = $r.Forms[0]
PS C:\>$form | Format-ListId : login_formMethod : postAction : https://www.facebook.com/login.php?login_attempt=1Fields : {[charset_test, ?,',?,',?,?,?], [lsd, ], [return_session, 0], [legacy_return, 1]...}PS C:\> $form.fieldsKey Value--- -----charset_test ?,',?,',?,?,?lsdreturn_session 0legacy_return 1displaysession_key_only 0trynum 1ug1j2t_1emailpasspersist_box 1default_persistent 0ug1j2t_2 Log In
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.
PS C:\> $form.Fields["email"] = "User01@Fabrikam.com"PS C:\>$form.Fields["pass"] = "P@ssw0rd"
The eighth command uses the Invoke-WebRequest cmdlet to log 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 (which was the session variable 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.
When the command completes, the StatusDescription property of the web response object in the $r variable indicates that the user is logged in successfully.
PS C:\> $r = Invoke-WebRequest -Uri $form.Action -WebSession $fb -Method POST -Body $form.FieldsPS C:\>$r.StatusDescriptionOK
Related topics