Summary: MVP, Thomas Rayner, continues to inspire holiday spirit by using PowerShell to trigger multimedia.
Yesterday in Sounds of the Holidays–Part 1, I tried to inspire my coworker, Matthew, with some cool holiday messages. Unfortunately, Matthew is still being a Grinch and I need to step up my game. Instead of some spoken messages from “Ebenezer Script,” I’m going to play him some holiday music every time he signs in to his workstation.
I’m a fan of the Charlie Brown holiday jazz album by the Vince Guaraldi Trio, and they’ve been kind enough to upload some of their music to YouTube. I’ve chosen this great piece to play for Matthew: Christmas Time Is Here.
My script for this is pretty simple. First, I’m going to use a couple of variables to define where the startup folder is on Matthew’s computer and the URL to this YouTube video. Be advised that anything in the startup folder is executed every time a user signs in to the workstation—so be careful with what you put in here.
$Computer = '\\Matthew-Computer\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp'
$MusicURL = 'https://www.youtube.com/watch?v=YvI_FNrczzQ'
I know how to launch Internet Explorer to a specific page. It’s as easy as:
iexplore.exe http://workingsysadmin.com
However, I want to capture that line in a variable without executing it, so this is what I’ll use:
$PlayMusic = @"
Start-Process -WindowStyle Minimized 'iexplore.exe' $MusicURL
"@
By using the @” <some text> “@ syntax to create a here-string, I’ve defined a block of text composed of the command to launch Internet Explorer at the URL of my YouTube video. I’ve decided to launch Internet Explorer as minimized. Now all I need to do is write that command to a file in Matthew’s startup folder:
$StartupScript = New-Item -ItemType File -Path "$Computer\PlayMe.ps1"
Set-Content -Value $PlayMusic -Path $StartupScript.FullName
This is the full script.
$Computer = '\\Matthew-Computer\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp'
$MusicURL = 'https://www.youtube.com/watch?v=YvI_FNrczzQ'
$PlayMusic = @"
Start-Process -WindowStyle Minimized 'iexplore.exe' $MusicURL
"@
$StartupScript = New-Item -ItemType File -Path "$Computer\PlayMe.ps1"
Set-Content -Value $PlayMusic -Path $StartupScript.FullName
It’s that easy! Now Matthew will be able to enjoy some nice holiday music every time he signs in to his computer. If that doesn’t raise his holiday spirits, nothing will!
~Thomas
Thanks again, Thomas! Always nice to have a little fun with PowerShell!
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. Until then, peace.
Ed Wilson, Microsoft Scripting Guy