Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Weekend Scripter: Cheesy Script to Set Speaker Volume

$
0
0

Summary: Set the speaker volume by using Windows PowerShell—the cheesy script way.

Microsoft Scripting Guy, Ed Wilson, is here. It is still the weekend, and that means I get a chance to play. Speaking of playing…

Yesterday the Scripting Wife and I were at the horse show in Ashville. For us, it was play. For the people participating in the show, it is very serious business. Anyway, I got to see my favorites: the Roadster Ponies. These little dudes are quick and fun to watch. Here is a cool picture I took:

Photo of horses

Anyway, speaking of cool…

Here is a cheesy script I wrote on the way back home for setting the volume of my speakers.

There is always a right way, a wrong way, a Windows PowerShell way, and a cheesy way to do things. The exact method I choose to do something depends on my personal use-case scenario. For something like setting speaker volume, I want to be able to programmatically set my volume to maximum or to minimum when I am listening to Internet radio, watching a video, or on a conference call.

Why is the script cheesy? Well, because it uses the old-fashioned SendKeys method from the old VBScript WshShell object. SendKeys can be very unreliable, and is not really recommended for automation solutions. But for something like this, it works just fine.

So why cheesy in the first place? Well, because really automating sound requires some heavy duty excursions into native code. A good example is a script by JRV in the Script Center Repository: PowerShell Set PC Volume Control.

So what does my script do?

I wrote a function that I can call from wherever I am in the Windows PowerShell environment. Obviously, I could not call it Set-Volume because that is already a CIM function in Windows 8 that deals with storage. So I called it Set-SpeakerVolume. The heart of the function is where I use SendKeys to send [char]174 (the down volume key on my laptop) or [char]175 (the up volume key on my laptop). 

By experimentation, I figured out that each key press raised or lowered the volume by two. Therefore I use the 1..50 array to send 50 key presses. The [char]173 key displays only the volume arrow, and it does not raise or lower the volume. I set this as the default action of the function so I can quickly see what the current volume is set to, as shown in the following image:

Image of menu

The complete Set-SpeakerVolume function is shown here:

Function Set-SpeakerVolume

{ Param (

  [switch]$min,

  [switch]$max)

 

  $wshShell = new-object -com wscript.shell

 

  If ($min)

  {1..50 | % {$wshShell.SendKeys([char]174)}}

  ElseIf ($max)

  {1..50 | % {$wshShell.SendKeys([char]175)}}

  Else

  {$wshShell.SendKeys([char]173)} }

Hope you are having a great day, and join me tomorrow for a new week on the Hey, Scripting Guy! Blog. Until then, take care.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles