For this article, I would encourage you to un-mute your speakers and keep them at a reasonable level,or use headphones in case you are going through this in a public area or at work so you do not bother your neighbors.
Which scenario SPEAK class will required?
IF when you have a script that’s going to run a long time, say 10-20 minutes. We all KNOW that we’re not going to sit there and watch the output!. In this scenario, PowerShell talk when some condition comes up and its is really useful if you’ve relegated the window to the background and
are doing something else like working with other items.
Example of Real Scenario:
In below sample code the script will start to check the 10 lakhs record for finding the detail of specific user. If it match with user id it start specking with user details, so it will be make aware of the details by informaging through voice, if we working with other items.
1 2 3 4 5 6 7 8 9 |
#Loop through 1000000 users foreach ($userId in $ListOfUsers) { if ($userId -eq '1456') { write-host "Got the User details" write-host $userId $speak.speakAsync("We found the User $($userId.FirstName) $($userId.LastName)") } |
Example: 1
The first thing that we want to do is create our SpeechSynthesizer object and then we can utilize the methods in it. During the execution of below script, the speak method talk which mentioned in the double quotes ie., “Hello from dotnet-helpers”.
1 2 3 4 |
ADD-TYPE -AssemblyName System.Speech $speak = new-object System.Speech.Synthesis.SpeechSynthesizer $speak.speakAsync("Hello from dotnet-helpers!") $speak.Speak("The current date and time is $(Get-Date)") |
If you wanted to increase the volume of the voice, you can adjust the Volume property from 0-100 on your object.You can even control the rate at which the words are spoken to you.
$speak.Volume = 100
If you want a faster response, you can also increase the rate to something like 10 using “Rate”.
Speed it up!
$speak.Rate = 10
$speak.speakAsync(“Hello from powershell!”)
Slow it down!
$speak.Rate = -10
$speak.speakAsync(“Hello from powershell!”)
What do you think?
I hope you have an idea of how to use the PowerShell SpeechSynthesizer during our long time execution for the output. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.