I am trying to use the Rename-Item
cmdlet to do the following:
I have folder which contains \"*.txt\" files. Let\'s say 1.txt, 2.txt et
As noted by @lytledw, Move-Item -Force
works great for renaming and overwriting files.
To replace the index.
part of the name, you can use the -replace
regex operator:
Get-ChildItem index.*.txt |ForEach-Object {
$NewName = $_.Name -replace "^(index\.)(.*)",'$2'
$Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
Move-Item -Path $_.FullName -Destination $Destination -Force
}
I tried the rename-item -force and it did not work. However, I was able to do move-item -force and it worked fine