Search code examples
powershellrenamerename-item-cmdlet

Powershell- Rename. Insert Number in File name


I have been trying to use Powershell to Rename folder directories in bulk.

I have many folders that are formatted ab_xxxxx_xxxxx_xxx (where x is a number) I would like it to be renamed to ab_xxxxxxx_xxxxxxx_100xxx.

I have trying using Rename Item with the Replace command and it adds the 100 after every _

Ex. Get-ChildItem -Path ab___* -Directory | ForEach-Object -Process { Rename-Item -Path $.Name -NewName ($.Name -replace "_", "_100") }

I also tried using the * wild card but it just added that to the new folder name.

Any Help would be greatly appreciated


Solution

  • I hope this could help

    Get-ChildItem $path "ab_*" | where {$_.PSIsContainer} | ForEach-Object { Rename-Item $_.name ($_.name.insert(($_.name.LastIndexOf("_")+1),"100")) }
    

    This powershell command will rename folder named "ab_67890_12345_323" to "ab_67890_12345_100323". I hope this what you want.