Translating Languages with PowerShell

When you come from another country, it can be difficult living in the Netherlands sometimes. Dutch is not as difficult as you’ll hear from others, but sometimes you want to make sure the sentence you’ve read means what you think. Mistakes can be embarrassing, expensive, or something that falls into the general I-should-have-checked-that-first category. Trust me, I know……..

Normally for translation I’ll use the Google Translate web site. The translation is not always 100% accurate, but usually enough to understand things. And it’s quick.

For automation, Google provide a web services API, but this is a paid-for service.  Fortunately, Microsoft provide a free translation web service, not costing you anything unless you plan to translate more than 1,000,000 characters a month.

There are some steps that we need to do to be able to use Microsoft’s translation web service before we can begin scripting.

Sign up for the Microsoft Translator Web Service

1.  Visit the Microsoft Azure Marketplace at

Azure Marketplace Front
.  Click Sign In at the top right of the screen.

Azure Marketplace Logon
3. Sign-in with your Microsoft account credentials (create a Microsoft account if you do not have one before continuing).

microsofttranslatorsearch4. Begin to type Microsoft Translator into the search box at the top right.
5. In the search list, click Microsoft Translator.

Microsoft Azure Marketplace
6. Click Sign Up on the free option.

7. Select the I have read and agree to the above publisher’s Offer Terms and Privacy Policy check box.
8. Click Sign Up.

Register your Application

Now it’s time to register our application which will be use the Microsoft Translator web service.
Register your application

1. In the Client ID box, type a name that you wish you be associated with your application. This can be a mixture of numbers, letters, hyphens and underscores.
2. In the Name box, type a suitable name for your application. This property is not used in the cmdlet we will be writing.
3. In the Client secret box, type a password for the application. Keep this information secure.
4. In the Redirect URI box, type any valid URL. This property is also not used in our script.
5. In the Description box, type a description if you wish for later reference.
6. Click CREATE

7. Select Sign Out.
8. Close your browser window.

Script the Cmdlet

With the previous steps complete, we can start to script the cmdlet. This cmdlet borrows code from MSDN, so I’d recommend visiting the Microsoft Translator pages for more information on the web service and examples.

The cmdlet uses input parameters of Text for the string to be converted, and From and To ones, which you use to indicate the original language the string is in, and the one you want it translated to. You can find the most recent list of supported languages at the Translator Language Codes webpage, also on MSDN.

I’ve enclosed an example below, together with the type of result you should expect.

Get-Translate -Text 'I speak English. I learn from a book' -From en -To nl


function Get-Translation
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True,Position = 0)] [string] $Text,
        [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)] [string] $From = 'nl',
        [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)] [string] $To = 'en'
    Begin {
        Add-Type -AssemblyName System.Web
        $ClientID = 'myclientid'
        $secret = 'mysecret'
        $encodedURL = [System.Web.HttpUtility]::UrlEncode($ClientID)
        $encodedSecret = [System.Web.HttpUtility]::UrlEncode($secret)
        $Uri = ''
        $Body = "grant_type=client_credentials&client_id=$encodedURL&client_secret=$encodedSecret&scope="
        $ContentType = 'application/x-www-form-urlencoded'
        $AccessToken = Invoke-RestMethod -Uri $Uri -Body $Body -ContentType $ContentType -Method Post
        $HeaderValue = 'Bearer ' + $AccessToken.access_token
        [string] $textEncoded = [System.Web.HttpUtility]::UrlEncode($Text)
        [string] $Uri = '' + $Text + '&from=' + $From + '&to=' + $To

        $result = Invoke-RestMethod -Uri $Uri -Headers @{
            Authorization = $HeaderValue
        $hash = @{
            OriginalText   = $Text
            TranslatedText = $result.string.'#text'
            From           = $From
            To             = $To
        New-Object -TypeName PsObject -Property $hash