41

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]

Before receiving this error, I copied and pasted the code from this MSDN page into the PowerShell ISE, and replaced the placeholder values with my actual credentials:

# ...
$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)
# ...

What additional code do I need to add for this to work?

David Bailey
  • 940
  • 1
  • 10
  • 21
  • 1
    did u already see this http://stackoverflow.com/questions/27898258/httputility-not-recognised-in-net-4-5 – Aravind Jul 16 '16 at 08:02

1 Answers1

95

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
vonPryz
  • 22,996
  • 7
  • 54
  • 65