How to use custom UserAgent with Invoke-WebRequest on Powershell?
Quote from HeelpBook on March 2, 2021, 12:13 amAs asked in the question, how to use/see which UserAgents to use when using Invoke-WebRequest?
As asked in the question, how to use/see which UserAgents to use when using Invoke-WebRequest?
Quote from HeelpBook on March 2, 2021, 12:24 amThe 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):
cls
$request = Invoke-WebRequest "https://www.whatismybrowser.com/detect/what-is-my-user-agent"
$request.AllElements | Where { $_.id -eq "detected_value" } | Select innerTextWhat 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) }}
We can use it like in the following command:
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
Invoke-WebRequest https://www.google.com -UserAgent $userAgent
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):
cls
$request = Invoke-WebRequest "https://www.whatismybrowser.com/detect/what-is-my-user-agent"
$request.AllElements | Where { $_.id -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) }}
We can use it like in the following command:
$userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
Invoke-WebRequest https://www.google.com -UserAgent $userAgent