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 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] ...} |
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] ) NotSet Male Female Neutral |
1 2 3 4 5 6 | PS D:\> [System.enum] ::GetNames( [System.Speech.Synthesis.VoiceAge] ) NotSet Child Teen Adult Senior |
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 .voice 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] ...} |
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:\LanguagesAndOptionalFeatures PS D:\> Dism /online / Add-Capability /capabilityname:Language.TextToSpeech~~~de-de~0.0.1.0 /source:D:\LanguagesAndOptionalFeatures |
Or from the Microsoft store.
Michael