Text-To-Speech using PowerShell

Just a little fun thing for a Friday, text-to-speech! We used this many years ago at the office, sending this to the laptop of a colleague and having fun while he’s trying to figure out where the voice is coming from 🙂

Table Of Contents
show

How it works

There is a System.Speech assembly, which can be used to convert text to speech, I made an Invoke-TextToSpeech function that accepts a -Text and a -Computername parameter. The -Computername starts an Invoke-Command to the computer name you specify and starts talking on the remote computer. It doesn’t have a volume control function (Yet. If anyone knows how… Let me know! ), so the speaker shouldn’t be muted on that target 🙂

Below is an example of running it locally:

Invoke-TextToSpeech -Text 'PowerShell is fun'

The voice is based on your local setting. In my case, it sounds pretty good in English. You can also output text from a variable in your script by running this:

$variable | Invoke-TextToSpeech

In the example below, it connects to a remote computer (Laptop-001) and outputs the text to that speaker:

Invoke-TextToSpeech -Text 'PowerShell is fun' -Computername Laptop-001

Note: There could be WinRM/Firewall settings preventing this on the remote computer

The script

Below is the Function that I made. Run it in your PowerShell session, and have fun 🙂

function Invoke-TextToSpeech {
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)][ValidateNotNullOrEmpty()][string]$Text,
        [parameter(Mandatory = $false)][string]$Computername
    )
    
    #If Computername is not specified, run local convert text to speech and output it
    if (-not $Computername) {
        try {
            Add-Type -AssemblyName System.Speech
            $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
            $synth.Speak($text)
        }
        catch {
            Write-Warning ("Could not output text to speech")
        }
    }

    #try to connect to remote computer, convert to speech and output it
    if ($computername) {
        try {
            Invoke-Command -ScriptBlock {
                Add-Type -AssemblyName System.Speech
                $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
                $synth.Speak($Using:Computername)
            } -ComputerName $Computername -ErrorAction Stop
        }
        catch {
            Write-Warning ("Could not connect to {0}" -f $Computername)
        }
    }
}

Download the script(s) from GitHub here

2 thoughts on “Text-To-Speech using PowerShell

    • You can only select the ones available on the system, you can find more information here about it : https://learn-powershell.net/2013/12/04/give-powershell-a-voice-using-the-speechsynthesizer-class/

      I could choose between these two, David or Zira.

      C:\Users\HarmVeenstra> $speak.GetInstalledVoices().VoiceInfo

      Gender : Male
      Age : Adult
      Name : Microsoft David Desktop
      Culture : en-US
      Id : TTS_MS_EN-US_DAVID_11.0
      Description : Microsoft David Desktop – English (United States)
      SupportedAudioFormats : {}
      AdditionalInfo : {[Age, Adult], [Gender, Male], [Language, 409], [Name, Microsoft David Desktop]…}

      Gender : Female
      Age : Adult
      Name : Microsoft Zira Desktop
      Culture : en-US
      Id : TTS_MS_EN-US_ZIRA_11.0
      Description : Microsoft Zira Desktop – English (United States)
      SupportedAudioFormats : {}
      AdditionalInfo : {[Age, Adult], [Gender, Female], [Language, 409], [Name, Microsoft Zira Desktop]…}

      I switched using the how-to on the page to $speak.SelectVoice(‘Microsoft Zira Desktop’) and got the female voice.

      In the script, you could change:

      $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
      $synth.Speak($text)

      to

      $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
      $synth.SelectVoice(‘Microsoft Zira Desktop’)
      $synth.Speak($text)

      but first check which ones you have available by:

      Add-Type -AssemblyName System.Speech$synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
      $synth.GetInstalledVoices().VoiceInfo

      The $synth.GetInstalledVoices().VoiceInfo shows you the options

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.