Monday, April 27, 2015

Make your PowerShell Script SPEAK : Use VOICE notifications in your PowerShell scripts.

 

Picture1

 

Who said, that only artists are creative?

We the coders are creative too. We use the white canvas of ISE and use the “Cmdlets” to write our own poetry, draw our imagination on it.

Believe me, we are really creative.

Anyway, It’s an era of “Voice Enabled” personal assistants.

Microsoft had introduced the “Cortana” in Windows 8.1 phones and in the Desktop version of Windows 10, is also containing “Cortana”, She speaks.

Do you know, we can make our PowerShell scripts to “Speak” too? Wondering how?

 

By using the SAPI.SpVoice com object.

SAPI means = Speech Application Programming Interface.

To use the SAPI voice engine, we need to create a new com object and save it to the variable.

$talkIt = New-Object -ComObject SAPI.SpVoice

In the above command, we are creating a new SAPI.SpVoice Object.

Our new $talkIt object has a method name SPEAK( ). Now we can, make our PowerShell scripts speak whatever we like them to say.

$talkIt.speak("Dear $env:Username the time is Date and time is $(get-date)"

In the above code, the script will speak my name and the current data and time, and everything is dynamic, nothing is hardcoded.

you can even see, that’s is very easy to use in-built environment variables and script blocks too.

$talkIt = New-Object -ComObject SAPI.SpVoice

$talkIt.speak("Dear $env:Username the time is Date and time is  is $(get-date)")

Try the code now and enjoy it.

 Cortana

Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

 


 

3 comments:

  1. Thanks Aman for this post!!
    Just I want to know, is this command will in only windows 10 PowerShell or it can be use in windows 8.1 also.

    Thanks
    Kashi

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. I tried to test the above code and it gave me the below error:

    PS C:\> $talkit = New-Object -ComObject SAPI.SpVOice
    PS C:\> $talkit.Speak("Dear vivek Uniyal the date and time is $(Get-Date)")
    Exception calling "Speak" with "1" argument(s): "Exception from HRESULT: 0x8004503A"
    At line:1 char:57
    + $talkit.Speak("Dear vivek Uniyal the date and time is $(Get-Date)")
    + ~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

    ReplyDelete