Powershell – Launching Media files within Powershell

| | Comments (1) | TrackBacks (0)

Powershell

I got a little bit inspired today.   If you saw an amazing Sharepoint Guru found out how to make Powershell talk by using  NEW-OBJECT and linking to the Speech interface in Windows.

Then all day long in the back of my head I kept thinking, “Now if I could get Powershell to Play Music, I could get it to do Karaoke”.

Yes it was an odd day, which spawned THIS function.  INVOKE-MEDIA

Really it’s no different that any other link to Com Objects.   I create a link to it using a Variable in Powershell and attaching to the Media Player in Windows.  Of course, if you don’t have the Media Player or a sound Card (or a computer for that matter) this will fail miserably.

But all I did to figure it out was

1) Dig into the registry under HKLM\Software\Classes for what appeared to be the Location of Windows Media Player and Guessed (Turned out to be called “MediaPlayer.MediaPlayer” for the COM name (well THAT wasn’t very creative!)

2) Created a link to it using

$PLAYER=NEW-OBJECT –ComObject ‘MediaPlayer.MediaPlayer’

3) Piped the results into a GET-MEMBER to see what Methods and properties were available.   Turns out quiite a few to understate.

The ones that caught my interest were “FILENAME” (A Property) and “Play” a Method.

So now executing the following new lines

$PLAYER.Filname=”C:\Windows\Media\chimes.wav”
$PLAYER.play()

Produced the the “Chimes.Wav” file playing in the background

To write this into a useful function just execute

function global:INVOKE-MEDIA ($Filename) {

$PLAYER=NEW-OBJECT –ComObject ‘MediaPlayer.MediaPlayer’
$PLAYER.play()

}

And to use that function just simply execute

INVOKE-MEDIA ‘C:\Windows\Media\Chimes.wav’

Or what so ever drives your fancy as a file.

Powershell, It’s easier than it seems

Sean
The Energized Tech

0 TrackBacks

Listed below are links to blogs that reference this entry: Powershell – Launching Media files within Powershell.

TrackBack URL for this entry: http://www.energizedtech.com/cgi-sys/cgiwrap/jolyrogr/managed-mt/mt-tb.cgi/307

1 Comments

invoke-item will also do the same thing. It will load the default media player and start playing.

Invoke-Item "C:\Windows\Media\chimes.wav"

Leave a comment