Posey's Tips & Tricks

How To Play Sounds with PowerShell

Instead of twiddling your thumbs as you wait for a long-running script to complete, you can have PowerShell play a sound to alert you when it's done.

I have always thought of PowerShell as being primarily a visual interface. Think about it for a moment: When you enter a cmdlet, PowerShell displays something on the screen in response, even if the output is only another PowerShell prompt, or even an error message.

However, it is also possible to use PowerShell to produce audible output.

Recently, I have been experimenting with adding auditory prompts to PowerShell scripts. As obnoxious as this probably seems, it does have its place. Some PowerShell scripts take a long time to run. I have, on occasion, built scripts that took upwards of 20 minutes to execute. For long-running scripts, it can be handy to get PowerShell to play a sound when the script completes. That way, you can go do something else while the script is running, but know the instant that the script completes.

The technique used for producing a basic tone is simple. Here is the command:

[console]:beep(1000,500)

So let's take a look at what this command is actually doing. Obviously, the beep portion of the command is what generates the sound, but you will notice that there are two numbers following the beep. The first of these numbers is 1,000. This number controls the pitch of the beep. You can experiment with different numbers to change the sound of the tone.

The valid range of these numbers is debatable. According to Microsoft, anything lower than 190 or higher than 8,500 is beyond the range of human hearing. On other Web sites, however, there are those who claim to be able to hear higher and lower frequencies.

Out of curiosity, I decided to put this to the test for myself. I will be the first to admit that everyone's hearing is different, and I have really good hearing, so your results may vary.

On the low end, I tried using a value of 50 and was able to hear the tone. That being the case, I decided to go with the ridiculously low value of 20. Doing so actually caused PowerShell to produce an error message saying that the lowest supported value was 37. I was able to hear tone number 37 with no problems.

The same error message indicated that the highest-frequency tone that PowerShell could produce was number 32,767.  I was able to hear this tone, too. So I think that the lesson here is to ignore Microsoft's recommendation to use tones between 190 and 8,500.

In case you are wondering, the second number used by the beep command controls the duration of the tone. I wasn't able to find anything that confirmed the correlation between this number and the number of seconds that the tone plays, but a value of 1,000 seems to be roughly about a second.

So what about the [console] portion of the command? Again, I couldn't find anything definitive, but this seems to be a way of redirecting output to the Windows console.

Being able to signal the end of a long-running script with a tone can obviously be useful. While researching this article, however, I stumbled onto a Web page where someone had created full-blown musical compositions using a series of beep commands in PowerShell. Personally, I'm not musically talented enough to pull that off, but it was an amusing technique nonetheless, and is something that you might want to experiment with.

Oh, in case you are wondering, there are ways of getting PowerShell to play sound files such as .MP# files or .WAV files. Unfortunately, there does not seem to be an easy way of doing this. PowerShell does not include a native media-playing cmdlet that I am aware of, but there are ways of playing sound files by writing PowerShell scripts that leverage .NET. From what I have heard, there are some people who have used these types of scripts combined with a series of audio files to make their PowerShell scripts talk.

About the Author

Brien Posey is a 16-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

comments powered by Disqus
Most   Popular

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.