Powershell Batch Rename

I keep forcing myself to use PowerShell from time to time as I know it'll pay big dividends if I just get over the initial (steep) learning curve. So, faced with the task of renaming some files in a folder today I turned to PowerShell. About an hour later I was still staring at the same set of files with the same names. I'd have been quicker using some sort of disk sector editing tool at the rate I was going.

I found plenty of help on the web - or at least plenty of suggestions for renaming and batch renaming. But everything I tried gave me an error - either that I was trying to call a method on a null valued expression or that the path didn't exist (oh yes it does).

After much mucking around, it turned out this was due to the presence of [] characters in the filename which also happen to be wildcard characters. And the rename-item cmdlet that I was trying to use sees them as a wildcard and as far as I can see there's no way to escape them. The solution is to use move-item instead which allows you to specify -literal and have square brackets mean just that. I ended up with:

 dir *_t*.gif | foreach { move-item -literal $_ $_.Name.Replace("_thumb[1]", "")}

If only I'd known what to search for I could probably have found this post first and saved myself some hair. I like PowerShell but I don't find any of it very intuitive. [Update: And I just realised that post is by Adrian Bateman who works at Microsoft in the UK]

3/10 - Must try harder (for me, not PowerShell).

Technorati Tags: powershell