PoshCode Logo PowerShell Code Repository

Add Voice to Powershell (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/667"></script>download | new post

Add voice to your Powershell script

  1. ###
  2. # Description: Add Voice to Powershell
  3. # Version: 1.1 (11 Nov 2008)
  4. # Mike Hays / www.mike-hays.net / blog.mike-hays.net
  5. # Virtualization, Powershell, and more...
  6. ###
  7.  
  8. # This is the actual speaking part.  I cheat by adding spaces
  9. # (This makes the word sound right).
  10. $spokenText = "Super ca li fragilistic expi alidocious"
  11.  
  12. # Create an object that represents the COM SAPI.SpVoice
  13. $voice = New-Object -com SAPI.SpVoice
  14.  
  15. # Get the list of available voices
  16. $voiceList = $voice.GetVoices()
  17.  
  18. # This script prefers using LH Michelle as a stand-in for Mary Poppins,
  19. # but I can't be sure that she exists on all computers, so I check for that.
  20. # She comes with some installations of Microsoft Word 2003.
  21. $voiceDescList = @()
  22. for ($i=0; $i -lt $voiceList.Count; $i++)
  23. {
  24.     $voiceDescList += $voiceList.Item($i).GetDescription()
  25. }
  26.  
  27. if ($voiceDescList -contains "LH Michelle")
  28. {
  29.     $voiceMember = "Name=LH Michelle"
  30. }
  31. else
  32. {
  33.     # This is the default voice if LH Michelle doesn't exist.
  34.     # This will probably be Microsoft Sam
  35.     $voiceMember = "Name=" + $voiceDescList[0]
  36. }
  37. $voiceToUse = $voice.GetVoices($voiceMember)
  38.  
  39. # This sets the voice property on the COM object
  40. $voice.Voice = $voiceToUse.Item(0)
  41.  
  42. # This actually does the speaking.
  43. [void] $voice.Speak($spokenText)
  44.  
  45. # She's no Julie Andrews, but she'll say what you want.
  46. # END

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me