Forrest logo
back to the Invoke-WebRequest tool

invoke-webrequest:tldr:42533

invoke-webrequest: Pass a username and password for server authentication.
$ Invoke-WebRequest -Headers @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("myusername:mypassword")) } ${http:--example-com}
try on your machine

This command is using the Invoke-WebRequest cmdlet in PowerShell to send an HTTP request to a specific URL (in this case, http://example.com).

The -Headers parameter is used to specify custom HTTP headers for the request. In this case, it is being used to include an Authorization header with a Basic authentication scheme.

To construct the Authorization header, the command uses the @{ } syntax to define a hashtable of headers. The key-value pair in the hashtable represents the name (Authorization) and value (Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("myusername:mypassword")) ) of the header.

The value of the Authorization header is constructed using Base64 encoding. The [System.Text.Encoding]::ASCII.GetBytes method is used to convert the string "myusername:mypassword" to ASCII byte array. Then, [System.Convert]::ToBase64String is used to convert the byte array to a Base64 encoded string.

The result is an HTTP request with the Authorization header set to "Basic ".

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the Invoke-WebRequest tool