Powershell – How to replace specific string in a file name using powershell

powershellpowershell-3.0

I am trying to write a powershell script which go to specific folder which have multiple folders containing Excel file. File name is as example
1_RIM_Reports_201510.xlsx
2_Rim_Reports_September.xlsx

Now I want to change replace the word RIM with NIR how can i do that for multiple files.

1_NIR_Reports_201510.xlsx
2_NIR_Reports_September.xlsx

Get-ChildItem $directory -Recurse |

    Rename-Item { $_.name -replace '*RIM*.xlsx', '*NIR*.xlsx' }

Best Answer

If your string always have the the underscore _ before and after you can simply use this:

Get-ChildItem $Directory -Recurse | 
Foreach-Object { Rename-Item $_.FullName ($_.FullName -replace "_RIN_","_NIR_")}

Otherwise you need to use Regular Expression, if this is the case let us know