Teaching PowerShell To Speak

Friday, 01 February 2013
  • By
  • Jeff Ammons
  • Tags:
  • Humor
  • PowerShell
  • Programming
  • Tips
  • Tools

This is a quickshot tip with some silliness thrown in for good measure.

One of the cool bits of PowerShell is that it is very easy to call COM objects from the command line and therefore in scripts.

I can’t take credit for any discovery here except to point you once again to the excellent video Continuous Integration: Char by char – PowerShell by Harald Fianbakken.

In his continuous integration script, he demonstrates calling the text-to-speech API built into Windows. That derailed me from finishing his video for an hour or more as I played with PowerShell and the speech API.

To start let’s see how we get hold of the API.

$speechy = New-Object –ComObject SAPI.SPVoice;

That’s it.

Now to make it speak we just do this:

$speechy.Speak(“This is a test”)

You want to see it again? How about in one line?

(New-Object –ComObject SAPI.SPVoice).Speak(“This is a different test”)

That is pretty easy.

On Windows 8 we have different voices available.

If you want to make a function that says something in each of the voices registered on your system, try this (link to this function on GitHub Gist):

function Out-Speech($text) {

  $speechy = New-Object –ComObject SAPI.SPVoice;

  $voices = $speechy.GetVoices();

  foreach ($voice in $voices) {
    $speechy.Voice = $voice;

Now you can simply call the function:

Out-Speech(“This is a test”);

If you've never created a function in PowerShell before, just remember to open the PowerShell ISE (Integrated Scripting Environment). That will let you write a script instead entering text one line at a time in the command line PowerShell console.

My next blog post will do something useful with this, but not this post. Oh no. Now we get silly.

You see way back before the dawn of time when a Macintosh was either a Macintosh or not (aka the mid ‘80s), I found a speech to text program on our school’s Mac.

After the novelty of making it swear wore off (two, maybe three years I think), I began to try to make it speak with an accent.

Me, I’m a hillbilly from North Carolina, so first thing I needed to do was make it threaten someone in as close to a southern Appalachian way as possible.

Through much experimentation with phonemes and other highly linguistical sounding hoo-ha, I arrived at the following (DO try this at home):

Out-Speech(“"Eye m gunna ki ick yur ice, you sum bee itch!"”)

This upset the speech API who replied:

Out-Speech("Oh no you dih-int.")

Next I tied the JFK API:

Out-Speech("Ahsk naught wot yawr con tree con dew far you, ahsk wot yew con dew far yawr con tree")

That kind of bored me so I fired up the SNES API:

Out-Speech("Eets a ME, Ah Mahd Eo")

Unfortunately the inner 3rd grader came out next:

Out-Speech("Oh, dear! My bum is itching most terribly! I must scoot around on the rug to scratch it! Oh yes! That is SPLENDID!")

At this point I noticed that the Zira voice sounds like a GPS turn-by-turn voice, so I create the WORST… GPS… EVAAR.

Out-Speech("Turn left, in 100 feet and plow into that bus full of nuns.")
Out-Speech("Accelerate up ramp and leap over all those stupid buses Evil K-neivel!")
Out-Speech("In 100 feet you will die in a fiery explosion of death and molten car parts. So says the Oracle Of The Dashboard!")