Skip to main content desktop
Try Now

PowerShell: Text-to-Speech Examples

Kris PowellKris Powell

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

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

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

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


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="" 
    <voice xml:lang="en-US">
    <prosody rate="1">
        <p>Normal pitch. </p>
        <p><prosody pitch="x-high"> High Pitch. </prosody></p>

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

This blog post is part of a series on PowerShell:

Ready to put the PowerShell Scanner through its paces?

Take our 14-day Free Trial. Feel the power of the PowerShell scanner.
Start a Trial

Don't miss the next post!

What Is the PowerShell Equivalent of Dsquery

Dsquery is a popular command to return objects from AD. Learn how to use the PowerShell equivalent, Get-ADObject, to make the most of your PowerShell scripts.

© 2021 Corporation
  • PDQ Deploy ®
  • PDQ Inventory ®
  • Simple MDM
  • Pricing
  • Downloads
  • Licensing
  • Buy