Kae Travis

Text-to-Speech using PowerShell and the Speech Synthesizer

This blog provides an example of how we can implement text-to-speech using PowerShell and the speech synthesizer.

Text-to-Speech using PowerShell

I remember the early days of making a computer convert text to speech in a robotic voice – there was nothing more satisfying than writing expletives directed at your siblings without actually saying the words yourself. Nowadays I even find joy in sending Alexa notifications to my wife asking her for a cup of tea with half a sugar (although she does become slightly irate after asking multiple times per day). Anyway, I digress.

This is a simple example of how we can make PowerShell convert text into speech. We can easily change the voice and the speed of the voice.

Add-Type -AssemblyName System.speech
#create soeech synthesizer
$speechSyn = ([System.Speech.Synthesis.SpeechSynthesizer]::New())
#-10 slow to 10 fast
$speechSyn.Rate = -0
#find available voices
#$speechSyn.GetInstalledVoices() | foreach {
#    write-host ($_.VoiceInfo).Name
#    write-host ($_.VoiceInfo).Culture  
#    write-host ($_.VoiceInfo).Age  
#    write-host ($_.VoiceInfo).Gender  
#    write-host ($_.VoiceInfo).Description  
#    write-host ($_.VoiceInfo).Id  
#    write-host ($_.VoiceInfo).AdditionalInfo  
#}
#select an available voice
$speechSyn.SelectVoice("Microsoft Hazel Desktop")
#say something
$speechSyn.Speak('This is a test.')
#dispose
$speechSyn.Dispose()

I’m using Windows 10 and it seems I only have “Microsoft Hazel Desktop” and “Microsoft Zira Desktop”. This is because in Settings > Time and Language > Speech I only have the English (United Kingdom) voice package installed. If I add the English (United States) voice package, I then get “Microsoft David Desktop” after restarting the PowerShell session.

Have fun!

Text-to-Speech using PowerShell and the Speech Synthesizer
Text-to-Speech using PowerShell and the Speech Synthesizer

Leave a Reply