God Loves PowerShell (and you too)

I’ve been wanting to work with PowerShell for a long time.  I’ve brought it up a few times but just played around with it.  I never actually did anything with it … that was until a couple of weeks ago.

Our church, Central Baptist Church, records the Sunday morning sermons and puts them online.  However, the sermons weren’t being uploaded. They were being recorded, but weren’t being uploaded and posted to the website.

So I grabbed all of the mp3s from the church’s AV computer and brought them home.  I then opened them in Audacity to chop of any excess content on the beginning and end of the sermons and re-exported them into the /processed/ subfolder.

As I exported them I set the MP3 tag properties like Album, Title, Year, Artist, etc.  This process definitely took the most time – opening each mp3 and normalizing them, stripping the beginning and/or end if needed and exporting them took the entire afternoon.  And now it was almost time for me to head out for the evening services.

Now I needed to actually open up Windows Live Writer to create the blog posts to save each of these sermons.

So my initial problem was that I just created these 49 files with their new filenames, title of the track (Sermon Name) and artist (Speaker Name) but I didn’t have an easy way to get that information back out when I’d create each individual blog post other than right clicking on each one and looking at the file details to see the MP3 tags.

I figured this would be a good problem for PowerShell to solve so I did a quick search for “get multimedia file details in powershell” and the first link that popped up in Bing was:

http://powershell.com/cs/blogs/tobias/archive/2011/01/07/organizing-videos-and-music.aspx

Using this as the starting point I created the following script:

$shell = New-Object -COMObject Shell.Application
$folder = 'D:UsersChadDocumentscbcsermonsprocessed'
$shellfolder = $shell.Namespace($folder)
$sermons = dir $folder

Which produced the following results:

image

I followed that up by

foreach($sermon in $sermons) {
    $shellfile = $shellfolder.ParseName($sermon)

    $shellfile.Name + ' - ' + $shellfolder.GetDetailsOf($shellfile, 21) + ' by ' +
        $shellfolder.GetDetailsOf($shellfile, 13)
}

And those results were:

image

I then just exported that to a text file I was able to read and then copy and paste the values I wanted.

I’m sure there is probably a pretty easy way to generate the blog posts with PowerShell itself either by interfacing with Windows Live Writer or Orchard CMS but I was in a hurry and didn’t have time to look at that.  I was happy with the time I was able to save by getting the data I needed.  I didn’t mind copying and pasting the data from Notepad to Windows Live Writer as I created the individual blog posts.  It would have been cooler to do it via a script, but I was more concerned about getting it done before walking out the door (in the next 10 minutes).

The point here is that I was able to very quickly get up and going with PowerShell.  If I had tons more of files to go through then I would have spent the time to figure out how to automatically create the blog post but that wasn’t my main concern.  Windows Live Writer made that process pretty easy to do manually.

Now that I did that small task, I constantly have a PowerShell window open now (and normally I have 3 different ones opened).  At work and with my personal projects I use Mercurial for source control and always used the command line but now I use PowerShell along with Posh-Hg.  I’ve been digging GitHub recently and am using PowerShell with Posh-Git to grab code from there.

I’ve noticed a lot of C# Console Apps I’ve made over the years could have been more easily done via PowerShell scripts.  Also being able to easily use .net objects I’ve made via PowerShell is also awesome.

So if you haven’t messed with PowerShell yet, but it is on your “to look at” list, then do yourself a favor and load it up and start using it – even for something small.  When all you have is a hammer, everything looks like nail.  Add PowerShell to your tool belt and you will soon see things you would have done a different way before but can now do more efficiently using PowerShell.

God loves you. But does God love PowerShell?  I don’t know, but I believe He is the Ultimate Engineer and so I think He’d dig it.  What I do know is that folks at my church are grateful to finally have the sermons online.  I’d have to think that would cause God to smile as well.

The entire script (with the core wrapped in a function) is below:

$shell = New-Object -COMObject Shell.Application
$folder = 'D:UsersChadDocumentscbcsermonsprocessed'
$shellfolder = $shell.Namespace($folder)
$sermons = dir $folder

function getSermonDetails {
    foreach($sermon in $sermons) {
        $shellfile = $shellfolder.ParseName($sermon)

        $shellfile.Name + ' - ' + $shellfolder.GetDetailsOf($shellfile, 21) + ' by ' +
            $shellfolder.GetDetailsOf($shellfile, 13)
    }
}

getSermonDetails > Sermon_Details.txt

Happy Scripting!

-Chad