Files
DevOps/Powershell/Speech/say.ps1
2025-09-13 20:36:34 +02:00

32 lines
884 B
PowerShell

# Check if the System.Speech assembly is available
try {
Add-Type -AssemblyName System.Speech
} catch {
Write-Error "Windows Text-to-Speech (SAPI5) is not installed or not available. Please ensure you have Windows 10/11 with the latest updates."
exit 1
}
# Check for command-line arguments
if (-not $args) {
Write-Error "Error: No text provided. Usage: .\speak.ps1 'Your Text Here'"
exit 1
}
# Use first command-line argument as text to speak
$text = $args[0]
# Create SpeechSynthesizer
$synth = New-Object System.Speech.Synthesis.SpeechSynthesizer
try {
#$synth.SelectVoice("Microsoft David Desktop")
$synth.SelectVoice("Microsoft Hazel Desktop")
#$synth.SelectVoice("Microsoft Zira Desktop")
# Speak the text
$synth.Speak($text)
#Write-Host "Text spoken successfully."
} catch {
Write-Error "Error speaking text: $_"
exit 1
}