Share this post:

Adam the Automator sent me down the rabbit hole when he posted about adding voice to your Windows PowerShell scripts using text to speech.

After walking through Francisco Navarro’s tutorial, I thought I’d have a little fun. There are two voices available by default in the EN-US version of Windows 10, David and Zira. David is a male voice and Zira is a female voice. This got me thinking, they should do a duet!

The SpeechSynthesizer class is native to the Windows .NET framework, so this will only work in Windows PowerShell.

# Define assembly path
$assemblyPath = 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\System.Speech.dll'

# Add assembly dll
Add-Type -Path $assemblyPath

# Create new objects
$david = New-Object System.Speech.Synthesis.SpeechSynthesizer
$zira = New-Object System.Speech.Synthesis.SpeechSynthesizer

# Change voice to Zira
$zira.SelectVoice('Microsoft Zira Desktop')

# Endless Love
$david.Speak("My love")
$david.Speak("There's only you in my life")
$david.Speak("The only thing that's bright")

$zira.Speak("My first love")
$zira.Speak("You're every breath that I take")
$zira.Speak("You're every step I make")

$david.speak("And I")
$david.speak("I want to share")
$david.speakasync("All my love with you") | Out-Null
$zira.speakasync("All my love with you") | Out-Null
$david.speak("No one else will do")

$zira.speak("And your eyes")
$david.speak("Your eyes, your eyes")
$david.speakasync("They tell me how much you care") | Out-Null
$zira.speakasync("They tell me how much you care") | Out-Null
$david.speakasync("Ooh yes, you will always be") | Out-Null
$zira.speakasync("Ooh yes, you will always be") | Out-Null
$david.speakasync("My") | Out-Null
$zira.speakasync("My") | Out-Null
$david.speakasync("Endless love") | Out-Null
$zira.speakasync("Endless love") | Out-Null

The great thing about doing things like this is that it makes learning fun. Even though the outcome might not be all that useful, I learned several things that I can apply in other more “productive” scripts. I learned about loading assemblies to access classes not natively available in Windows PowerShell. I experimented with multiple instances of an object (David and Zira). I also learned about exploring an objects properties and how a property can be an embedded object with it’s own properties. I also got to experiment with executing commands synchronously and asynchronously to get my duet to “sing” together.

P.S. If you really want it, I posted the full version on GitHub. 🤣
Endless Love by Microsoft David and Microsoft Zira

Share this post:


Comments are closed