Give The Power of Speech and Sound to Your PowerShell Scripts

Do you ever have the problem where you start a long running script (such as running a code build), multi-task on something else on another monitor while waiting for the script to finish, and then totally forget about the script until half an hour later? Well, here’s a solution your problem: have your script give you holler at you when it’s done.

Hello sir! I am done running your command!

In my library script file, I have the following functions to play sound files and to speak any text:

function PlayMp3($path) {
    # Use the default player to play. Hide the window.
    $si = new-object System.Diagnostics.ProcessStartInfo
    $si.fileName = $path
    $si.windowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $process = New-Object System.Diagnostics.Process
    $process.startInfo=$si
    $process.start() 
}

function PlayWav($path) {
    $sound = new-Object System.Media.SoundPlayer;
    $sound.SoundLocation="$path";
    $sound.Play();
}

function Say($msg) {
    $Voice = new-object -com SAPI.SpVoice
    $Voice.Speak($msg, 1 )
}

If you like the text-to-speech feature but find Windows’ speech engine lacking, check out Ivona. It’s a commercial text-to-speech engine but you are allow to generate and download short speech files for free personal use. Now, my script can nicely interrupt me to tell me when it’s done. Other online text-to-speech engines: vozMe, SpokenText.

If Making Noise Is Not Your Thing

If making noise is not your thing, consider displaying a message in the Notification Area. Here’s the code (courtesy Microsoft TechNet):

Build complete!

function Get-ScriptName {
    $MyInvocation.ScriptName
} 

function DisplayNotificationInfo($msg, $title, $type) {
    # $type - "info" or "error"
    if ($type -eq $null) {$type = "info"}
    if ($title -eq $null) {$title = Get-ScriptName}

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 
    # Specify your own icon below
    $objNotifyIcon.Icon = "C:CdoScriptsFolder.ico"
    $objNotifyIcon.BalloonTipIcon = "Info"  
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.BalloonTipText = $msg

    $objNotifyIcon.Visible = $True 
    $objNotifyIcon.ShowBalloonTip(10000)
}

3 Replies to “Give The Power of Speech and Sound to Your PowerShell Scripts”

  1. If you are going to create the notify object, its also a good idea to remove it.

    I have some code below with an example.

    Using –> $objNotifyIcon.Dispose

    I wrote this code for PowerShell v1.0 and also some of my function names do not follow the correct verb-noun naming conventions.

    ########################
    function ShowNotify_Icon([string]$msg)
    {
    $objNotifyIcon.BalloonTipIcon = ‘Info’
    $objNotifyIcon.Icon = ‘D:\Create_Notify_Icon\graph.ico’
    $objNotifyIcon.BalloonTipTitle = ‘Processing …’
    $objNotifyIcon.BalloonTipText = $msg
    $objNotifyIcon.Visible = $True
    $objNotifyIcon.ShowBalloonTip(9000)
    Sleep(9)
    }#ShowNotify_Icon

    ########################
    function HideNotify_Icon([string]$msg)
    {
    $objNotifyIcon.BalloonTipText = $msg
    $objNotifyIcon.ShowBalloonTip(9000)
    Sleep(9)
    $objNotifyIcon.Visible = $false
    }#HideNotify_Icon

    ########################
    #Start Here : )
    [void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon

    ShowNotify_Icon(‘This operation is starting . . .’)

    HideNotify_Icon(‘This operation has completed . . .’)

    $objNotifyIcon.Dispose
    #main

  2. Eveгythinbg is vᥱry open with a precise description of the issues.
    It was really informative. Yoour site is еxtremely helрful.
    Thank you for sharing!

    Feel free tto surf to my web blog: rom

Leave a Reply

Your email address will not be published. Required fields are marked *