Search code examples
windowspowershellcmdfile-renamebatch-rename

Batch Replace Character in File Names


I have about 600 video files with the $ special character in the title.

I want to batch replace this with a letter s.

I found the powershell code online below and it works fine with replacing letters with other letters but fails when trying to replace the $ special character

get-childitem -recurse | rename-item -newname { $_.name -replace "",""}

I tried using the code below and it ends up adding an s to the end of the file type instead of replacing the $

get-childitem -recurse | rename-item -newname { $_.name -replace "$","s"}

$hortvid.mp4 becomes $hortvid.mp4s instead of shortvid.mp4

Any ideas on how to get this to work correctly?


Solution

  • Just use \ escape character:

    When running line in the directory:

    get-childitem -recurse | rename-item -newname { $_.name -replace "\$","s"}
    

    input file:

    $hortvid.mp4
    

    output file is renamed:

    shortvid.mp4