The small joys of using PowerShell

I was looking for some good tech talks to download for the weekend and came across “Techdays 2012 the Netherlands”. I went ahead and downloaded a few of the videos using the Firefox extension “DownThemAll”.

Once the downloads were complete, this is what my folder contents looked like:

image

That was as expected, but I’d rather prefer the names to be just the names of the talks without the Techdays….. prefix. And, I wasn’t really keen on renaming each one of them by hand, coz I plan to download more Smile

PowerShell to the rescue!

PowerShell was the first thing that came to my mind for automating this. After a bit of binging around for syntax, I started off and came up with this script:

 $files = ls -name -exclude *.ps1
  
 foreach ($f in $files)
 {
     $d = [regex]::split($f, 'Netherlands');
     
     Rename-Item $f $d[1]
 }
  
 ls -name

 

At this point, I went all philosophical, thinking “Sometimes, it’s the little things in life that makes you happy” Smile

And, I’m new to PowerShell – this is my second “useful” script (I’ll blog about the first one shortly). I’d love to learn more about it and would like to hear from you, Dear Reader, if you would have written this script any differently.

 

Thanks for reading!

Amar

 

PS: You can follow me on twitter at “_amarnit”, where I share some quick tips/learnings at times! Smile

Comments

  • Anonymous
    May 04, 2012
    every little helps. But PowerShell is very powerful
  • Anonymous
    May 04, 2012
    There is a built in -split operator in powershellhelp about_splitAnd powershell usually likes to be a pipeline instead of conventional loops, like sols -name -exclude *.ps1 | % {   $d =$f -split "Netherlands"           Rename-Item $f $d[1]}Powershell is severely under used and extremely powerful
  • Anonymous
    May 06, 2012
    Manas - Thank you and yes, PowerShell is awesomeNiclas - I can't believe I missed pipelines! :) Thanks for pointing that out - so used to conventional programming that I've lost touch with my scripting skills. And I couldn't agree with you more on the last sentance - POWERShell is called so for a reason, right ;)