Text To Voice With PowerShell

The other day I had a little down time at work so I decided to work on my PowerShell skills. In the process of playing around I came across a site that had a Text-To-Voice PowerShell game. I was presently surprised because I had never thought of doing any text to voice scripts before.

I use browser text to speech extensions all the time for things like proofreading. I’m¬†dyslexic to some extend and for me it’s easier to catch a mistake if I hear it then if I’m reading it myself.

I also use it to read webpages for me. So it sounded like a cool idea to be able to write my own program to do this.

Using that PowerShell game’s code as an example I wrote a simple program.

Text to Voice PowerShell Script

Add-Type -AssemblyName System.Speech
$voice = New-Object System.Speech.Synthesis.SpeechSynthesizer
write-host("---Voices available---")
#Looking for voices available on the computer
$voice.GetInstalledVoices() | ForEach-Object {
    if ($_.VoiceInfo.Id -match "TTS_MS_(?\w{2}-\w{2})_(?[^_]+)") {
        $Name = $matches.name
        $Culture = [cultureinfo]$matches.culture | select -expand DisplayName
    }
    else {
        Write-Warning "Couldn't get info from $($_.VoiceInfo.Id)"
        $Name = "ERROR"
        $Culture = "ERROR"
    }
    write-host("$Name. Speaking style is $Culture")
    #You could probably add something here to be able to select the voice you want.
    #I'm just using the last one in the list.
    $voice.SelectVoice($_.voiceinfo.name)
    #$voice.Speak("Voice Check")
}
while ($true){
    $text = Read-Host "Enter text to read: "
    $voice.Speak($text)
    }

I’m very happy with it. Now I can have a text to voice speech program without having to install any software. This will make the security team at my job happy.

What about Linux?

But now my mind is wondering how hard it would be to implement something like this on my Linux computers at home. I know PowerShell can be installed on Linux but I have never done that and I’m not sure if I want to.

Maybe it can be done in Python? I guess that will be a project for another day.

Leave a Reply

Your email address will not be published. Required fields are marked *