{"id":3148,"date":"2013-07-28T00:01:00","date_gmt":"2013-07-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/28\/weekend-scripter-cheesy-script-to-set-speaker-volume\/"},"modified":"2013-07-28T00:01:00","modified_gmt":"2013-07-28T00:01:00","slug":"weekend-scripter-cheesy-script-to-set-speaker-volume","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-cheesy-script-to-set-speaker-volume\/","title":{"rendered":"Weekend Scripter: Cheesy Script to Set Speaker Volume"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Set the speaker volume by using Windows PowerShell&mdash;the cheesy script way.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is still the weekend, and that means I get a chance to play. Speaking of playing&hellip;<\/p>\n<p>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:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8863.hsg-7-28-13-0.png\"><img decoding=\"async\" title=\"Photo of horses\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8863.hsg-7-28-13-0.png\" alt=\"Photo of horses\" \/><\/a><\/p>\n<p>Anyway, speaking of cool&hellip;<\/p>\n<p>Here is a cheesy script I wrote on the way back home for setting the volume of my speakers.<\/p>\n<p>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.<\/p>\n<p>Why is the script cheesy? Well, because it uses the old-fashioned <strong>SendKeys<\/strong> method from the old VBScript WshShell object. <strong>SendKeys<\/strong> can be very unreliable, and is not really recommended for automation solutions. But for something like this, it works just fine.<\/p>\n<p>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: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/PowerShell-Set-PC-Volume-1737b160\" target=\"_blank\">PowerShell Set PC Volume Control<\/a>.<\/p>\n<p>So what does my script do?<\/p>\n<p>I wrote a function that I can call from wherever I am in the Windows PowerShell environment. Obviously, I could not call it <strong>Set-Volume<\/strong> because that is already a CIM function in Windows 8 that deals with storage. So I called it <strong>Set-SpeakerVolume<\/strong>. The heart of the function is where I use <strong>SendKeys<\/strong> to send <strong>[char]174<\/strong> (the down volume key on my laptop) or <strong>[char]175<\/strong> (the up volume key on my laptop).&nbsp;<\/p>\n<p>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 <strong>[char]173<\/strong> 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:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6567.hsg-7-28-13-01.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6567.hsg-7-28-13-01.png\" alt=\"Image of menu\" \/><\/a><\/p>\n<p>The complete <strong>Set-SpeakerVolume<\/strong> function is shown here:<\/p>\n<p style=\"padding-left: 30px\">Function Set-SpeakerVolume<\/p>\n<p style=\"padding-left: 30px\">{ Param (<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [switch]$min,<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [switch]$max)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $wshShell = new-object -com wscript.shell<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; If ($min)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {1..50 | % {$wshShell.SendKeys([char]174)}}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; ElseIf ($max)<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {1..50 | % {$wshShell.SendKeys([char]175)}}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Else<\/p>\n<p style=\"padding-left: 30px\">&nbsp; {$wshShell.SendKeys([char]173)} }<\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Set the speaker volume by using Windows PowerShell&mdash;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&hellip; Yesterday the Scripting Wife and I were at the horse show in Ashville. For us, it was play. For [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[438,439,440,3,61,45],"class_list":["post-3148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-audio","tag-com","tag-media","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Set the speaker volume by using Windows PowerShell&mdash;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&hellip; Yesterday the Scripting Wife and I were at the horse show in Ashville. For us, it was play. For [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3148"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}