alarm clock windows media player via PowerShell

Run a scheduled task that invokes a PowerShell script to create an alarm clock to play your music using media player.

The modern personal computer is so powerful, yet using it as a alarm clock is not so easy. There are a few commercial and free “PC Alarm Clocks” available, so all is not lost.

But, there is already a media player in Windows and a task scheduler, so it should be a piece of cake to schedule Windows Media Player to play a song or playlist every morning.

One way not to do it is to just schedule Media Player to start up and play a song as shown in this article ““Windows Media Player Alarm Clock using Task Scheduler“. As the author of the article states:

The only problem with this alarm clock is that if you are LOGGED OUT or the system is in standby prior to activating, it will run WMP as admin and you wont even see it open when you log in. To turn it off you will have run Task Manager and actually kill the process. A small price to pay for the reliability of a loud clock ….

What we need is a programmatic solution, and fortunately Windows now has a decent scripting language, PowerShell, with access to the Windows features. Thus, we use a script (from “Weekend Scripter: A Cool Music Explorer Script“):

Add-Type -AssemblyName presentationCore

$mediaPlayer = New-Object system.windows.media.mediaplayer
$path = "C:\Users\Public\Music\jazz\Oregon\1000 Kilometers"
$files = Get-ChildItem -path $path -include *.mp3,*.wma -recurse

foreach($file in $files)
{
 "Playing $($file.BaseName)"
  $mediaPlayer.open([uri]"$($file.fullname)")
  $mediaPlayer.Play()
  Start-Sleep -Seconds 10
  $mediaPlayer.Stop()
} 

And save it as a ps1 type file, for example: c:\batch\PlayMedia.ps1
In the above script, change the file so that ‘$path’ points to your own music folder. Note that the script plays each mp3 it finds for only ten seconds. How would you change it to just play the whole work?

Next you’ll open up the Task Scheduler and create a new basic task. The Actions tab is where you’ll edit how to run the task. So, enter into the dialog box:

Action:  Start a program
Program/script:  powershell
Add arguments (optional): -command "& 'C:\batch\PlayMedia.ps1' " 

Of course, set the days and times, and you can even select that the system will wake the computer to run the task.

I learned how to run PowerShell in scheduler at “How to schedule a Windows Powershell script“.

Advantages?
Well, now that you can program the system to act like a musical alarm clock, you can tweak it to use playlists, radio, etc. Soon you’ll be getting it to wake you up with “A Night On Bald Mountain” shaking the house, and while you are running around the bedroom screaming from fright, the PC will calmly start a batch of whole wheat pancakes and pouring on the real maple syrup; slowly the aroma from the coffee it started brewing will start to permeate your consciousness , and no, that giant Chernabog is not coming for you.

What is PowerShell?
Surprisingly, many technical people have never heard of PowerShell. This is Microsoft’s innovation in their management infrastructure. Before, the “command shell” and associated batch language were really decades old DOS-like behavior. That shell could not compare to Unix/Linux shells like bash. PowerShell is an object-based scripting language and shell. Afaik, it is unique in that instead of using only text manipulation, as in most unix pipe examples, it pipes objects.

Updates
Oct 11, 2013: I found the perfect system, Sonos. This is a premium wireless music system. It also can do sleep timer and alarm clock function.

System

  • Windows 7 64bit Professional
  • Windows Powershell 2.0

Further Reading

Leave a Reply

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