#PSTip PowerShell and the pre-configured user agent strings

Note: This tip requires PowerShell 3.0 or above.

The Invoke-WebRequest and Invoke-RestMethod cmdlets have the -UserAgent parameter, so you can specify a user agent string for the web request. A user agent string has “Compatibility (Platform; OS; Culture) App” format, and by default, PowerShell 3.0 identifies itself as “Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) WindowsPowerShell/3.0” on my Windows 7 machine. 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:

PS> [Microsoft.PowerShell.Commands.PSUserAgent].GetProperties() |
Select-Object Name, @{n='UserAgent';e={ [Microsoft.PowerShell.Commands.PSUserAgent]::$($_.Name) }}
Name             UserAgent
----             ---------
InternetExplorer Mozilla/5.0 (compatible; MSIE 9.0; Windows NT; Windows NT 6.1; en-US)
FireFox          Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) Gecko/20100401 Firefox/4.0
Chrome           Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6
Opera            Opera/9.70 (Windows NT; Windows NT 6.1; en-US) Presto/2.2.1
Safari           Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16

We can use it like in the following command:

PS> $userAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome
PS> Invoke-WebRequest https://powershellmagazine.com -UserAgent $userAgent
Share on: