main

Bulk Rename Files and Folders Rapidly using Windows PowerShell

65,000 files batch-renamed in less than a minute!

How to quickly and easily find and replace characters in file or directory names in Windows?

To bulk rename files and folders should be easy.

I wanted a quick solution, that didn’t require that I have great coding skills, and where I wouldn’t have to wrestle with installing a feature-rich program and wrestling with its interface. Just something I can do, easily, now.

It turns out that I can batch rename file and folder names with PowerShell.

The Problem

I am uploading images into OneDrive, and then I get this error

OneDrive needs your attention – 314 items can’t be synced

image

This message will also pop up when right clicking on OneDrive and checking “view sync problems”

image

Investigating a little deeper I discover that in each file name there is a hash # which OneDrive doesn’t like

image

It turned out that there were 65,000 of these files with a hash in the file name

image

image

The Solution – using PowerShell

With this simple code I can find the hash in any file name in directories and subdirectories and replace it with an underscore.

$dir = "C:\Users\Alan\OneDrive - CalmIT\Photos"
cd $dir
Get-Childitem -Recurse | `
Where-Object {$_.Name -match '#'} | `
Rename-Item -Newname { $_.Name -replace '#','_' }

  1. Specify the target directory = C:\Users\Alan\OneDrive\Photos
  2. Specify the search string to be found in both the “find” and “replace” sections of the code = #
  3. Specify the string to be replaced =  _
  4. Copy and Paste into PowerShell and hit Enter
  5. 65,000 instances batch-renamed in less than a minute

image

image

Thank you to Jamie Hutchinson’s YouTube video 

If you can improve on this please add your suggestions in the comments.

No comments yet.

Leave a Reply

Share