Wednesday, April 23, 2025
HomePowershellTextual content-To-Speech utilizing PowerShell - PowerShell is enjoyable :)

Textual content-To-Speech utilizing PowerShell – PowerShell is enjoyable :)


Just a bit enjoyable factor for a Friday, text-to-speech! We used this a few years in the past on the workplace, sending this to the laptop computer of a colleague and having enjoyable whereas he’s making an attempt to determine the place the voice is coming from 🙂

The way it works

There’s a System.Speech meeting which can be utilized to transform textual content to speech, I made an Invoke-TextToSpeech operate that accepts a -Textual content and a -Computername parameter. The -Computername begins an Invoke-Command to the pc title that you just specify and begins speaking on the distant laptop, it doesn’t have a quantity management operate (But, if anybody is aware of how… Let me know! ) so the speaker shouldn’t be muted on that focus on 🙂

Beneath is an instance for working it regionally:

Invoke-TextToSpeech -Textual content 'PowerShell is enjoyable'

The voice is predicated in your native setting, in my case, it sounds fairly good in English. You can even output textual content from a variable in your script by working:

$variable | Invoke-TextToSpeech

Within the instance beneath it connects to a distant laptop (Laptop computer-001) and outputs the textual content to that speaker:

Invoke-TextToSpeech -Textual content 'PowerShell is enjoyable' -Computername Laptop computer-001

Word: There could possibly be WinRM/Firewall settings stopping this on the distant laptop

The script

Beneath is the Operate that I made, run it in your PowerShell session, and have enjoyable 🙂

operate Invoke-TextToSpeech {
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)][ValidateNotNullOrEmpty()][string]$Textual content,
        [parameter(Mandatory = $false)][string]$Computername
    )
    
    #If Computername isn't specified, run native convert textual content to speech and output it
    if (-not $Computername) {
        attempt {
            Add-Kind -AssemblyName System.Speech
            $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
            $synth.Communicate($textual content)
        }
        catch {
            Write-Warning ("Couldn't output textual content to speech")
        }
    }

    #attempt to hook up with distant laptop, convert to speech and output it
    if ($computername) {
        attempt {
            Invoke-Command -ScriptBlock {
                Add-Kind -AssemblyName System.Speech
                $synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
                $synth.Communicate($Utilizing:Computername)
            } -ComputerName $Computername -ErrorAction Cease
        }
        catch {
            Write-Warning ("Couldn't hook up with {0}" -f $Computername)
        }
    }
}

Obtain the script(s) from GitHub right here

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments