Renaming in PowerShell: Underscore to Space

Special thanks goes to Jeffery Hicks for the original post in his blog, The Lonely Administrator. The following explains how to use Windows PowerShell to rename a group of files, so that all underscore characters (“_”) become space characters.

Quick start

To replace underscore characters with space characters in all files in a folder and all its subfolders:

  1. Open Windows PowerShell. This program is included with Windows 7 and is available free of charge for Windows XP.
  2. Navigate to the folder in which your files reside. To do so, type
  3. Type the following four lines of commands. You will need to press ENTER key after having typed each line. You also need to press ENTER key one more time to signal the conclusion of a multi-line command and prompt PowerShell to execute them.
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "_"} | foreach {
$New=$_.name.Replace("_"," ")
Rename-Item -path $_.Fullname -newname $New -passthru
}

Under the hood

The first line consists of three commands, separated by pipe (“|”) characters. The pipe character delivers the output of one command into another.

The first command is dir. (It is an alias for Get-ChildItem.) It lists all files in the current folder. Its parameter, -recurse, specifies that files in subfolders should also be listed. You can omit it, if you do not want subfolders.

The second command is where. (It is an alias for Where-Object.) It filters the results of dir command, discarding some and keeping others. In this case, only those items that not folders and have an underscore in their names are kept. The rest are discarded.

The third command is foreach command. It iterates through the list of files received from where command and runs two commands in succession on them. These commands are enclosed between a “{” and a “}”. The first of these subcommands, seen on line two, acquires the file name, replaces all underscore characters in it with a space character and remembers it. This new name henceforth referred to as “$New“. The next and last subcommand is Rename-Item command. It changes the name of the file received from where command to the name that is now internally known as “$New“.

Advertisements

Posted on 1 July 2012, in Windows Administration and tagged , , , , . Bookmark the permalink. 7 Comments.

  1. What about ignoring the extension if, say, you want to remove the periods in file names and replace them with spaces?

    IE: “The.Only.Way.avi” to “The Only Way.avi”

    • John Dangerbrooks

      Ah! That is going to add a couple more lines of code in place of line 2. You see how line two invokes .name property of file object (in this case “$_”)? There are other properties besides .name. We have .BaseName and .Extension. Line 2 can become something like:
      &newBase=$_.BaseName.Replace("."," ")
      $New=-join(newBase,$_.Extension)

      Let’s hope this works! (I didn’t test it.)

  2. Lars Panzerbjørn

    Interesting. But what if you just wanted to remove a leading Space or Underscore?
    I have been searching, but neither Replace nor .Trim() seems to do the trick for me -_-

  3. Hi there! I could have sworn I’ve visited this blog before but after browsing
    through a few of the articles I realized it’s new to me.
    Anyhow, I’m definitely delighted I stumbled upon it and
    I’ll be book-marking it and checking back often!

  4. Isnt there an easier way to replace ‘.’ with ‘ ‘? I have seen a couple of examples using Get-ChildItem to ID the directories with ‘.’, then use Replace after a | command? However I cant get any to work…

  5. Thank You! You saved my day.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: