Unable to find type [System.Web.HttpUtility] in PowerShell

David Bailey picture David Bailey · Jul 16, 2016 · Viewed 25.6k times · Source

I'm trying to obtain an access token for a Microsoft Translator application by using PowerShell, but certain commands in the process fail as a result of the error:

Unable to find type [System.Web.HttpUtility]

At first I typed out the code but the same error shows if I copy-paste the code directly from the MSDN page into the PowerShell ISE (and replace the missing values):

# ...
$ClientID = '<Your Value Here From Registered Application>'
$client_Secret = ‘<Your Registered Application client_secret>'

# If ClientId or Client_Secret has special characters, UrlEncode before sending request
$clientIDEncoded = [System.Web.HttpUtility]::UrlEncode($ClientID)
$client_SecretEncoded = [System.Web.HttpUtility]::UrlEncode($client_Secret)
# ...

I'm new to PowerShell (usually use Linux for development) but my guess was that this should work out-of-the-box without having to install additional tools; if not, where can I find them?

Answer

vonPryz picture vonPryz · Jul 16, 2016

You need to load the System.Web assembly. Use the Add-Type cmdlet like so,

PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
Unable to find type [System.Web.HttpUtility].

PS C:\> Add-Type -AssemblyName System.Web
PS C:\> [System.Web.HttpUtility]::UrlEncode("www.google.com")
www.google.com