PowerShell Pipeline

Talking Through PowerShell

Boe shows you how to write scripts using the built-in voices on your computer. Just make sure you've got your headphones on.

Sometimes a little fun is needed when you are working on or writing a script.

In this case, I am going to show you how to speak through your code by using PowerShell and the System.Speech.Synthesis.SpeechSynthesizer class.

For this article, I would encourage you to un-mute your speakers and keep them at a reasonable level, or use headphones in case you are going through this in a public area or at work so you do not bother your neighbors.

The first thing that we want to do is create our SpeechSynthesizer object so we can explore the object and figure out what we need to do next. Before that, though, we need to add the System.Speech assembly required to get to this class.

Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer

With this object created, we can take a look at the members of this object to see what we can do with it.

[Click on image for larger view.]

There are a lot of cool things here to look at! We can ignore the events as they won't be needed. Some methods that are interesting to me are Speak() and SetOutputToWaveFile(), and properties to review are Rate, Volume and Voice.

I'll start out by looking at the Speak method, which allows your computer to speak by simply providing some text with the method.

$speak.Speak("This is a test of some PowerShell code")

Assuming you have headphones in or the volume is turned up enough, you will get to hear the text that you wrote spoken back to you. You can even have the current date and time spoken to you, as well.

$speak.Speak("The current date and time is $(Get-Date)")

If you wanted to increase the volume of the voice, you can adjust the Volume property from 0-100 on your object.

$speak.Volume = 100

You can even control the rate at which the words are spoken to you. If you want a faster response, increase the rate to something like 10. Or you can slow it down by making it -10. By the way, these are also the limits for what you can set the rate to.

Speed it up!

$speak.Rate = 10
$speak.Speak("The current date and time is $(Get-Date)")

Slow it down!

$speak.Rate = -10
$speak.Speak("The current date and time is $(Get-Date)")

If you look at the Voice property, you can figure out that this particular one is a male voice named Microsoft David.

[Click on image for larger view.]

There is also a female voice that you can select named Microsoft Zira Desktop, which you can find when you view all of the currently installed voices using the GetInstalledVoices method.

[Click on image for larger view.]

Changing to a different voice is as simple as calling SelectVoice and providing the full name that you wish to use.

[Click on image for larger view.]

Now you can provide some text to Speak() and listen to  Zira tell you the date and time!

Lastly, you can save your voice work to a file by using SetOutputToWaveFile() and providing a path and file name that ends in .WAV to save the voice to a file that you can use.

[Click on image for larger view.]

With that, you can now take this knowledge and write your own scripts that utilize the built-in voices on your computer to talk to you or provide spoken alerts.

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus
Most   Popular

Upcoming Training Events