How To Make PowerShell Speak (Text-to-Speech .NET Tutorial)

Here is a quick and easy method for setting up text-to-speech in PowerShell.

Brien Posey

July 5, 2024

8 Min View

In this video, PowerShell expert Brien Posey demonstrates how to enable text-to-speech in PowerShell using .NET. He shows that you can achieve this with just three lines of code:

Add-Type -AssemblyName System.Speech
$Talk = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$Talk.Speak('With dot net you can make PowerShell speak')

The following transcript is edited for clarity and length.

Transcript:

Brien Posey: Hello, greetings and welcome. I'm Brien Posey. In this video, I want to show you how you can leverage .NET to make PowerShell talk. In other words, we will be enabling text-to-speech within PowerShell. Surprisingly, this is easy to do. You can do it with about three lines of code.

.NET Namespace, Class, and Method

First, I want to mention that we are leveraging .NET to get this done. I recently wrote a series of articles on using .NET within PowerShell.

If you missed those, here’s what this involves.

I opened my web browser to the .NET API browser. It’s where you can look at all the various .NET namespaces, classes, and methods. So, the namespace that we will use is called System.Speech.Synthesis.

I am going to go ahead and click on it. It takes me to the System.Speech.Synthesis namespace page. Right away, you can see all the classes that exist within this namespace. Now, the class that we will use is called SpeechSynthesizer. I will go ahead and click on this. Then, we can see some examples and all the properties and methods that apply to this class.

Related:ChatGPT Integration in PowerShell (Scripting Demo)

Now, the method that we will use is called Speak. And you can see the Speak method right here. If I click on this, it opens a page with some coding samples. Coding samples are written in C, not in PowerShell, but that is okay because it is easy to leverage this within PowerShell.

Why Use .NET?

Before I show you how this works, I want to answer a big question: Why would you use .NET to do this?

Well, there are two main reasons why you might want to use .NET within a PowerShell script.

One reason is that .NET will allow you to do things that you cannot do using native PowerShell. For example, I don’t know of a PowerShell cmdlet that supports text-to-speech. If we want to enable text-to-speech, we have to leverage .NET.

The other reason is that .NET can simplify the coding. After all, sometimes it might be possible to do something using native PowerShell but easier using .NET. Now, I do not have an example of how it is long and cumbersome to enable text-to-speech through PowerShell without .NET and how .NET makes it easy. As I said a moment ago, PowerShell does not natively support text-to-speech. However, I can show you an example from quite some time ago.

Related:How To Create Your Own PowerShell Cmdlets (Video Tutorial)

When I was a kid growing up in the ‘80s, I had an old RadioShack Color Computer. Back then, it was possible to do text-to-speech, but you had to have a text-to-speech cartridge. While preparing to record this video, I found a copy of the instruction manual for that sound speech cartridge online.

Here, you can see the Color Computer Speech Sound Cartridge instruction manual. The main thing that I wanted to show you is what's involved in creating text-to-speech. You will notice a couple of columns – decimal and hex – and a series of commands. You manipulated individual bits to enable text-to-speech on the old RadioShack Color Computer using the Speech Sound Cartridge. You had to do so based on a reference table.

So, what does all this look like in programmatic form? I don't want to go deeply into this, but I will give you a code sample. The instruction manual provided a sample program that allows you to do text-to-speech. At first glance, this doesn't look all that complicated. But you will notice that we have some statements in this program called Peek and Poke. In the old RadioShack basic, a Peek statement would look at a particular memory address, and a Poke statement would insert a value into a specific memory address. Right here, we have an If statement based on a Peek. We are looking at a particular memory address, checking to see its value, and then returning a value based on the value in that memory address. Likewise, the Poke statement you see here inserts a value into a specific memory address. So, even though this program doesn't look all that complicated at first glance, it uses direct bit manipulation in memory.

Related:How To Use PS2EXE To Convert PowerShell Scripts Into EXE Files

At least in the past, enabling text-to-speech programmatically tended to be a complicated endeavor. In the case of PowerShell, though, it's super simple to do, thanks to .NET.

How To Enable Text-to-Speech in PowerShell

So, how does this work? As mentioned, we will use the Speak method from the .NET and class SpeechSynthesizer. On the surface, this looks like a complicated example. However, in PowerShell, we only need three lines of code.

Let's go ahead and look at those three lines of code.

Add-Type -AssemblyName System.Speech
$Talk = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$Talk.Speak (‘With dot net you can make PowerShell speak’)

Remember, System.Speech.Synthesis is a .NET namespace. SpeechSynthesizer is a .NET class.

Let me go ahead and switch over to PowerShell. I'll go ahead and run the script. I'm getting a security warning. I'll press R to run it …

PowerShell Voice:With dot net you can make PowerShell speak.

Posey: … And you can see that PowerShell indeed spoke.

Use Cases for Text-to-Speech

Now, the big question: Why might you want to use something like this?

Well, this can be convenient in the case of long-running scripts. For example, in my environment, I have a backup script and could see myself embedding a verbal prompt at the end of the script to let me know when it's finished running. With that, I’ll know when my backups are complete without looking at the screen.

You might also embed a verbal prompt in a long-running script in response to a warning condition or an error condition that may have occurred. So that way, if you have a script that you've left unattended but that is within earshot, you can hear when something goes wrong, as opposed to having to periodically get up and look at the script to make sure that it's still running.

At any rate, that's how you enable text-to-speech within PowerShell using .NET.

I'm Brien Posey. Thanks for watching.

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like