PowerShell command to insert 1 character in the middle of a filename

Solution 1:

I guess the problem is that the pipeline runs while Get-ChildItem is still enumerating the files. You should probably first enumerate all files, and then rename them, so that renamed files don't get picked up twice:

(dir) | rename-Item -new { $_.name -replace '_','_1' }

The parentheses ensure that dir is first evaluated completely and its result then passed through the rest of the pipeline. it's conceptually the same as

$files = dir
$files | rename-item ...

Another option would be to check whether you need to rename the file at all:

dir | where { $_.Name -match '_\d{4}\D' } | rename-item -NewName { ... }

Solution 2:

ls | Rename-Item -NewName {$_.name.Substring(0,WHERE_TO_INSERT) + "STRING_TO_BE_INSERTED"  + $_.name.Substring(WHERE_TO_INSERT)}

You can use this one to insert whatever you want as a string inside the filename. Works on a whole folder.