Download Podcasts with Powershell
A number of people have mentioned to me that they didn't realize that Powershell is included by default in Windows 7. If you haven't yet jumped on the Powershell bandwagon, this is a good time. Powershell 2 includes a bunch of cool features like remoting (kind of like SSH) as well as a visual IDE for writing, editing and interactively debugging Powershell scripts.
Powershell great for system administration, but I mostly use it for quick and dirty "portable" apps that I don't feel like writing C#/VB for. Plus, I'm using .NET anyway, so it's all the same.
I wanted to download all my podcasts with Powershell, so I wrote this quick script in about 5 minutes. Other improvements I (or preferably you) could make to it could be: check the file size against the enclosure and re-download partials, rename the files to included a version of the title, include a progress bar.
Here's what I came up with. Perhaps you'll find it useful if you're not an iTunes/Zune person:
cd "C:\users\scottha\desktop\Hanselminutes Complete Download"
[Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath
$a = ([xml](new-object net.webclient).downloadstring("http://feeds.feedburner.com/HanselminutesCompleteMP3"))
$a.rss.channel.item | foreach{
$url = New-Object System.Uri($_.enclosure.url)
$file = $url.Segments[-1]
$file
if (!(test-path $file))
{
(New-Object System.Net.WebClient).DownloadFile($url, $file)
}
}
Of course you'll want to change the first line and the RSS Feed URL as you like.
If you've never used Powershell before, note that it's locked down from running scripts be default. You'll need to run it as Administrator once and run
Set-ExecutionPolicy unrestricted
This opens it up to run scripts, but it's not only VBS, the scripts won't run if you double-click them. You need to run powershell then type the name of your script to run it:
.\myscript.ps1
You can always set the execution policy back if it bothers you.
Hope this primitive mass podcast enclosure downloader is useful.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
BTW - RemoteSigned is a safer option than Unrestricted.
Set-ExecutionPolicy RemoteSigned
That way local scripts run without problem, but downloaded scripts need a signature or to be explicitly unblocked by the user.
Thanks for blogging about this. I'm a big fan of Powershell for automating anything that I have to do more than once.
Can you please guide me where to start to learn powershell from basics?
Thanks
Anandraj.A.
Lee
It must have been developed by an incomplete convert from Linux command line religion.
Why not some cleaner syntax, c# style?
Abomination: [Environment]::CurrentDirectory=(Get-Location -PSProvider FileSystem).ProviderPath...
or New-Object... why not New-NET-Framework-Clr-Object?
Regards
Mohit Thakral
Comments are closed.