Category Archives: .ps1

Batch Renaming Files in Powershell

I needed to rename a number of files, and include a number at the end.  eg:

Rename the following files:

IMG_20140725_143539.jpg
IMG_20140816_130503.jpg
IMG_20140816_130511.jpg
IMG_20140816_130513.jpg
IMG_20140818_143330.jpg
IMG_20140818_143331.jpg
IMG_20140818_143337.jpg
IMG_20140818_143357.jpg
IMG_20140818_143721.jpg
IMG_20140908_144527.jpg
IMG_20140908_144543.jpg
IMG_20140908_144549.jpg
IMG_20140909_070841.jpg
IMG_20140909_070849.jpg
IMG_20140909_111920.jpg
IMG_20140909_111927.jpg
IMG_20140909_122855.jpg
IMG_20140909_122910.jpg
IMG_20140909_122935.jpg
IMG_20140909_122945.jpg
IMG_20140909_122950.jpg
IMG_20140909_122953.jpg
IMG_20140909_123106.jpg
IMG_20140909_123115.jpg

 
to

stormpic01.jpg
stormpic02.jpg
stormpic03.jpg
stormpic04.jpg
stormpic05.jpg
stormpic06.jpg
stormpic07.jpg
stormpic08.jpg
stormpic09.jpg
stormpic10.jpg
stormpic11.jpg
stormpic12.jpg
stormpic13.jpg
stormpic14.jpg
stormpic15.jpg
stormpic16.jpg
stormpic17.jpg
stormpic18.jpg
stormpic19.jpg
stormpic20.jpg
stormpic21.jpg
stormpic22.jpg
stormpic23.jpg
stormpic24.jpg

I created a powershell script to do this, available here: http://pastebin.com/kTUyivgA

I used the command:

.renamefiles.ps1 -filepath c:temprenfile -renameto stormpic -leadingzeros $true
 

Run a Powershell script (.ps1) from command line


To run a Powershell script from the command line, batch file or task scheduler, use the following:
powershell -command “. ‘%SCRIPT%'”
Note the double quotes (“) followed by single quotes (‘), and the reverse on the end of the line.
eg:
powershell –command  “. ‘c:maintenancetuesday.ps1′”
To load a custom powershell console, for example the Exchange Management Shell, use the following:
powershell –PSConsoleFile %PSC1% –command “. ‘%SCRIPT%'”
eg:
powershell –PSConsoleFile “c:program filesmicrosoftexchange serverv14binExSchell.psc1” -Command “. ‘c:maintenanceReportMailboxSizeExchange2010.ps1′”