Hi,
Windows has a builtin feature to convert text to speech and play it over the speakers. This can be used in powershell by using the System.Speech namespace.
First of all include the namespace
1 | PS D:\> Add-Type -AssemblyName System.Speech |
Then create an instance of the SpeechSynthesizer class
1 | PS D:\> $SpeechEngine = New-Object System.Speech.Synthesis.SpeechSynthesizer |
Make a first test
1 | PS D:\> $SpeechEngine.Speak("Passt schon") |
Get all installed language details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | PS D:\> $SpeechEngine.GetInstalledVoices() | Select-Object -ExpandProperty VoiceInfoGender : MaleAge : AdultName : Microsoft David DesktopCulture : en-USId : TTS_MS_EN-US_DAVID_11.0Description : Microsoft David Desktop - English (United States)SupportedAudioFormats : {}AdditionalInfo : {[Age, Adult], [Gender, Male], [Language, 409], [Name, Microsoft David Desktop]...}Gender : FemaleAge : AdultName : Microsoft Zira DesktopCulture : en-USId : TTS_MS_EN-US_ZIRA_11.0Description : Microsoft Zira Desktop - English (United States)SupportedAudioFormats : {}AdditionalInfo : {[Age, Adult], [Gender, Female] , [Language, 409], [Name, Microsoft Zira Desktop]...} |
Select the voice which fits your needs (must be supported bei the installed language)
1 2 3 4 5 | PS D:\> [System.enum]::GetNames([System.Speech.Synthesis.VoiceGender])NotSetMaleFemaleNeutral |
1 2 3 4 5 6 | PS D:\> [System.enum]::GetNames([System.Speech.Synthesis.VoiceAge])NotSetChildTeenAdultSenior |
Set the output details
1 | PS D:\> $SpeechEngine.SelectVoiceByHints([System.Speech.Synthesis.VoiceGender]::Female,[System.Speech.Synthesis.VoiceAge]::Adult,1,"en-US") |
Show whats set
1 2 3 4 5 6 7 8 9 10 11 | PS D:\> $SpeechEngine.voiceGender : FemaleAge : AdultName : Microsoft Zira DesktopCulture : en-USId : TTS_MS_EN-US_ZIRA_11.0Description : Microsoft Zira Desktop - English (United States)SupportedAudioFormats : {}AdditionalInfo : {[Age, Adult], [Gender, Female], [Language, 409], [Name, Microsoft Zira Desktop]...} |
Additional languages can be installed by download the Language pack DVD. For example german
1 2 | PS D:\> Dism /online /Add-Capability /capabilityname:Language.Speech~~~de-de~0.0.1.0 /source:D:\LanguagesAndOptionalFeaturesPS D:\> Dism /online /Add-Capability /capabilityname:Language.TextToSpeech~~~de-de~0.0.1.0 /source:D:\LanguagesAndOptionalFeatures |
Or from the Microsoft store.
Michael