Forum Navigation
Please to create posts and topics.

How to use custom UserAgent with Invoke-WebRequest on Powershell?

As asked in the question, how to use/see which UserAgents to use when using Invoke-WebRequest?

The Invoke-WebRequest and Invoke-RestMethod cmdlets have the -UserAgent parameter, so you can specify a user agent string for the web request.

To get the actual UserAgent string used by the Powershell installed on the system we can execute the following code (in Powershell):

$request = Invoke-WebRequest ""
$request.AllElements | Where { $ -eq "detected_value" } | Select innerText

What values should we specify if we want to customize the user agent string? Static properties of the [Microsoft.PowerShell.Commands.PSUserAgent] class provide some pre-configured values:

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

Powershell Predefined UserAgent

We can use it like in the following command:

$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
Invoke-WebRequest -UserAgent $userAgent