Skip to content

How to set up text-to-speech using PowerShell

Black and White PDQ logo
Kris Powell|Updated January 21, 2021
PowerShell: Text-to-Speech Examples
PowerShell: Text-to-Speech Examples
Sections
    No Data

    This is a fun way to utilize Text to Speed (TTS) with PowerShell for Halloween. This is a fun little prank you can use to turn your users computers into “haunted” computers as long as they’ve got their speakers on. 

    We use the cmdlet Add-Type to add a .NET framework type to a PowerShell session. We can then instantiate a SpeechSynthesizer object to turn our text into audio output. It’s pretty exciting.

    The easiest way to try this is like so:

    Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer $speak.Speak('Hello...')

    You can even jazz it up a bit by modifying the speed of speech by modifying the rate property of the object. Here’s a good resource to see the different properties/methods of this object – link (scroll towards the bottom).

    Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer $tts.Rate = -5 # -10 to 10; -10 is slowest, 10 is fastest $speak.Speak('Hello...')

    From there, we can start having some fun. For example, we could place a list of phrases into an array or a separate text file and pull a phrase at random to be synthesized. Be sure to choose which method you’d like to use.

    Add-Type -AssemblyName System.speech $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer # Importing from text file $PhraseLocation = "C:\SomeFolder\ListOfPhrases.csv" $Phrase = (Get-Content $PhraseLocation) | Get-Random # Using an array $Phrase2 = @("I'm a robot.", "Help, I'm stuck in the machine!", "What are you doing, $env:USERNAME") | Get-Random $tts.Rate = 0 # -10 is slowest, 10 is fastest $tts.Speak($Phrase)

    Hopefully, this gives you some fun ideas for Halloween tomorrow!

    Bonus:

    Those of you who want to experiment with even more control over TTS can make use of Speech Synthesis Markup Language (SSML).

    Here’s an example:

    Add-Type -AssemblyName System.speech $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer $Phrase = ' <speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US"> <voice xml:lang="en-US"> <prosody rate="1"> <p>Normal pitch. </p> <p><prosody pitch="x-high"> High Pitch. </prosody></p> </prosody> </voice> </speak> ' $tts.SpeakSsml($Phrase)

    Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?

    Black and White PDQ logo
    Kris Powell

    Kris was an employee at PDQ.

    Related articles