Category filter
PowerShell script to convert text-to-speech
Text-to-speech (TTS) conversion is a technology that can transform written text into spoken words, enabling a computer or device to read out any text. It can be highly beneficial in various workplaces, particularly for IT administrators who are responsible for managing multiple devices. With TTS technology, IT administrators can send audio alerts and notifications to help the users stay informed about critical tasks and updates. This, in turn, can help with troubleshooting and communication with users. One way to achieve text-to-speech conversion is by using a PowerShell script. Through this script, IT administrators can automate the process of converting text-based content into audio format and easily transmit it to the intended users’ devices. Learn how to deploy a script using Hexnode’s Execute Custom Script action to convert text to speech on Windows devices.
PowerShell script
Execute the following script to convert the text specified within the script into speech and play the resulting audio on the device.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
try{ # Add speech assembly Add-Type -AssemblyName System.Speech # Determining the current logged in user $currentLoggedInuser="" $signedInUsers=get-wmiobject win32_process | where-object {$_.processname -eq "explorer.exe"} | Foreach ({$_.getowner().user}) foreach($user in $signedInUsers) { $isactive = C:\windows\system32\qwinsta.exe $user if($isactive -like "* Active*") { $currentLoggedInuser = $user break } } if($currentLoggedInuser -eq "") { Write-Host "cannot find any logged in user, so skipping script execution`nTo execute this script, atleast one user must be logged-in to the device" } else { Write-Host "Audio information shared for user: ",$currentLoggedInuser $script = "Start-Process ""$file"" -PassThru -Wait" # Unregistering ShowNotificationTask task from task scheduler (if we have already registered using script execution) try { $a=Unregister-ScheduledTask -TaskName "ShowNotificationTask" -Confirm:$false -ErrorAction:Ignore } catch { Write-Host "error while unregistering sheduletask-->",$_.Exception.Message } # Scheduling task $synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer $synthesizer.Speak("Hi, Welcome to Hexnode") $actions = (New-ScheduledTaskAction -Execute 'powershell.exe' -WorkingDirectory C:\Hexnode\Logs -Argument "-ExecutionPolicy unrestricted -WindowStyle Hidden -NoLogo -command `"&{(New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer).Speak}`"") $principal = New-ScheduledTaskPrincipal -UserId $currentLoggedInuser -RunLevel Highest $settings = New-ScheduledTaskSettingsSet -WakeToRun -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -Hidden -AllowStartIfOnBatteries -Priority 0 -StartWhenAvailable $task = New-ScheduledTask -Action $actions -Principal $principal -Settings $settings $reg= Register-ScheduledTask 'ShowNotificationTask' -InputObject $task Start-ScheduledTask -TaskName "ShowNotificationTask" -Verbose } } catch { Write-Host "Exception inside running script-->",$_.Exception.Message } |
The script above utilizes the System.Speech.Synthesis.SpeechSynthesizer
class to convert text to speech. To specify the text for conversion, you must include it immediately following this class in the code snippet. For instance, the example code provided above will produce the speech output “Hi, Welcome to Hexnode” as it is specified after the class.