0

Powershell Text To Speech | How To Guide

Today we’re going to go over how to use Powershell text to speech. This means that we can write a bunch of text and have Powershell basically say the words out loud through your speakers.

In some cases this might be useful to get an audio reminder for something or even if you want to have text read back to you. If that’s the case, we’ll share how use this feature.

Preparing Powershell to use Speech

To get started we’ll need to make sure we utilize the proper .NET Frameworks on your machine. The ones we need to focus on are the System.Speech type assembly. This will utilize the SpeechSynthesizer object to be able to use the built in voices that we’re looking for to use Powershell voice.


Add-Type -AssemblyName System.Speech
$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer

Add System.Speech Type Assembly

 

Using Text to Voice Manually

Before I dive into the script I wanted to showcase some items that you should probably be aware of behind the scenes. For starters, there are 2 built-in voices that come equipped with Windows 10. They’re named David and Zira.

David is the male voice while Zira is the female. In my opinion I like hearing Zira so I set it as the default in the script, but you’re more than welcome to change it up.

Now that we have the SpeechSynthesizer object created and set in the $Speech variable, we can check out some of the properties and methods that belong to it. For starters, we can pipe the $Speech to Get-Member and view the available methods there .

Powershell Voice Get Member
 

Next we want to be able to see what voices are available to us so we’ll use the GetInstalledVoices method like so.

$Speech.GetInstalledVoices() | select -ExpandProperty VoiceInfo | select Name, Gender, Description

 
Finally, if we wanted to change the voice from one voice to another, e.g. David to Zira, we can use the SelectVoice method.

$Speech.SelectVoice("Microsoft Zira Desktop")

Get Installed Voices Powershell1
 

Putting Powershell Speech All Together

The last thing we need to do it setup the actual text so Powershell knows what to say. We’ll put our message in a $Message variable and insert that into the Speak method.

$Message = 'This is awesome.  We now know how to use Powershell Text To Speech'
$Speech.Speak($Message)

Powershell Text To Speech Script

Now that we know how to do it manually, let’s leverage Powershell to do this automatically. After all, Powershell is a scripting language so it’s really suitable for our situation. We’ll name this function New-TextToSpeechMessage.


Function New-TextToSpeechMessage {
<#
.SYNOPSIS
    This will use Powershell to have a message read out loud through your computer speakers.


.NOTES
    Name: New-TextToSpeechMessage
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2021-Feb-28

.LINK
    https://thesysadminchannel.com/powershell-text-to-speech-how-to-guide -

.EXAMPLE
    New-TextToSpeechMessage -Message 'This is the text I want to have read out loud' -Voice Zira
#>
    [CmdletBinding()]
    param(
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]

        [string]    $Message,


        [Parameter(
            Position = 1,
            Mandatory = $false
        )]

        [ValidateSet('David', 'Zira')]
        [string]    $Voice = 'Zira'
    )

    BEGIN {
        if (-not ([appdomain]::currentdomain.GetAssemblies() | Where-Object {$_.Location -eq 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Speech\v4.0_4.0.0.0__31bf3856ad364e35\System.Speech.dll'})) {
            Add-Type -AssemblyName System.Speech
        }
    }

    PROCESS {
        try {
            $NewMessage = New-Object System.Speech.Synthesis.SpeechSynthesizer

            if ($Voice -eq 'Zira') {
                $NewMessage.SelectVoice("Microsoft Zira Desktop")
            } else {
                $NewMessage.SelectVoice("Microsoft David Desktop")
            }
    
            $NewMessage.Speak($Message)
    
        } catch {
            Write-Error $_.Exception.Message
        }        
    }

    END {}
}

So hopefully this article for going through the steps of using Powershell text to speech was able to help you out. I know I don’t use it on a daily basis but it is something that can come in handy from time to time. Not only that, it’s good to spruce up your Powershell skills so that’s another benefit to the script.

If you want to see more articles like this, be sure to check out our Powershell Gallery for other real world scripts. Don’t forget to check our Youtube Channel for other sysadmin video content.

5/5 - (4 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

Leave a Reply

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