Sunday, April 6, 2014

Powershell Intro

Was excited to learn that a friend's son is going to a technology camp this summer.  Thought he might like this little bit of powershell code.

The code at the end of this post will ask what you want the computer to say, and then it will speak the text.  Assuming you have a Windows computer, do the following to run the code:

1) Click on the Start button --> and type Run
2) type in     Powershell ISE
3) Copy and paste the code at the bottom of the post into the powershell window (should look similar to the screen shot below)


4) Press F5 (or the icon that looks like a DVD Play button)


Code to copy and paste:
# ------------------------------------------------------
#   ignore this for now
# ------------------------------------------------------
$ErrorActionPreference= 'silentlycontinue'
[Reflection.Assembly]::LoadWithPartialName('System.Speech')
# ------------------------------------------------------
#   clear the screen
# ------------------------------------------------------
cls
# ------------------------------------------------------
#   Ask what you want the computer to say and put that
#   info in the variable $phrase
# ------------------------------------------------------
$phrase = read-host -prompt "What do you what the computer to say?"
# ------------------------------------------------------
#   ignore this for now
# ------------------------------------------------------
$object = New-Object -ComObject SAPI.SpVoice
# ------------------------------------------------------
#   Write the variable $pharse to screen (not needed)
# ------------------------------------------------------
write-host $phrase
# ------------------------------------------------------
#   Have the computer speak what was entered
# ------------------------------------------------------
$object.Speak($phrase)