Renaming Files

Renaming Files

  • Comments 16

Let's have a little fun with Rename-Item:

[3112:0]PS> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\jps


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2007   4:21 PM        948 a
-a---          3/5/2007   4:21 PM       1214 b
-a---          3/5/2007   4:21 PM       1480 c


[3112:0]PS> dir |Rename-Item -NewName {$_.Name + "1"}
[3112:0]PS> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\jps


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2007   4:21 PM        948 a1
-a---          3/5/2007   4:21 PM       1214 b1
-a---          3/5/2007   4:21 PM       1480 c1

[3112:0]PS> foreach ($f in dir) {Rename-Item $f -NewName ($f.Name + "2")}
[3112:0]PS> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\jps


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2007   4:21 PM        948 a12
-a---          3/5/2007   4:21 PM       1214 b12
-a---          3/5/2007   4:21 PM       1480 c12

[3112:0]PS> dir |%{Rename-Item $_ $($_.Name +"3")}
[3112:0]PS> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\jps


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2007   4:21 PM        948 a123
-a---          3/5/2007   4:21 PM       1214 b123
-a---          3/5/2007   4:21 PM       1480 c123

[3112:0]PS> dir | %{$x=0} {Rename-Item $_ -NewName "Base$x"; $x++ }
[3112:0]PS> dir


    Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp\jps


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2007   8:24 PM       1086 Base0
-a---          3/5/2007   8:24 PM       1398 Base1
-a---          3/5/2007   8:24 PM       1710 Base2


Enjoy!

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post
  • One of my more frequently used scripts is a regex based file renaming function. It provides a preview of the new filenames and requests confirmation. (I haven't tried -whatif scripts yet, this is a generic preview) It's not the fanciest bit of code, but anyone is welcome to use it.

    http://www.birnamdesigns.com/option,com_content/task,view/id,173/Itemid,61/

  • Is the example

    dir |%{Rename-Item $_ $($_.Name +"3")} meant to be

    dir |%{Rename-Item $_ ($_.Name +"3")}. Note the missing $ in front of the brackets?

  • Ah thanks, I was looking for that. I've been using mv instead.

    Do you think you could alias Rename-Item to rename for the next version?

  • > dir |%{Rename-Item $_ $($_.Name +"3")} meant to be

    > dir |%{Rename-Item $_ ($_.Name +"3")}. Note the missing $ in front of the brackets?

    They are equivalent.

    Jeffrey Snover [MSFT]

    Windows PowerShell/MMC Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • When do you need the preceeding $ sign?

  • @ Fred

    Please refer to Arul Kumaravel's blog post for the differences on http://blogs.msdn.com/arulk/archive/2005/07/15/439459.aspx

  • Got It!  Now I can attack those other scripts with multiple "=" in them!  Thank you, Fred J.

  • I notice this is not taking into account file extensions? Would be nice to show how to rename the base name whilst maintaining the extension!

  • Hope you don't mind me asking a question here?  I have a challenge that my limited knowledge is struggling with.  I need to rename several hundred files that contain parenthesis in the file name.   example: c:\Important Document (Version 1).txt needs to be renamed as c:\Important Document-v1.txt

    I have tried the following and lots of variations with escape characters without sucess:

    dir | rename-item -newname{$_.name -replace " (Version 1).txt", "-v1.txt"

  • I like what this does. But I don't think I can use this for Windows 2000 Server. What I can I use as a scripting tool for Windows 2000 server?

  • I would like to see how to rename an extension given that you don't necessarily know the extension before hand but you do know a part of the file name.

  • How would you Select-String for files containing a certain string and then rename that group of files?

    What I am attempting to do is something similar to the code below. As you can see the code does NOT work.

    Select-string *.txt -pattern "subject: IT WO" | Replace-Item -NewName {$_.name -replace ".txt", "_itwo.txt"}

  • To rename all files in a folder to uppercase:

    $f = "$HOME\myFiles\"

    dir $f | Rename-Item -NewName {$_.Name.ToUpper()}

  • Thanks for { $_ }

  • I found it very useful to use the combination of rename and forfiles commands for renaming files/extensions.  I learnt it from here :www.windows-commandline.com/rename-file-extensions-bulk

Page 1 of 2 (16 items) 12