I am trying to rename a file Members.csv
.
Get-ChildItem ".\" -Filter '*Members*.csv' | where {
$_.LastWriteTime.GetDateTimeFormats()[44] -eq $today
} | Rename-Item -NewName "hello2.csv" -Force
-Force
is not working. If hello2 exists already the renaming will not take place.
I found this thread saying that we have to use Move-Item
. but I am not sure how to incorporate it in my code.
rename-item and override if filename exists
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 am not trying to replace, I'm renaming the entire name so I don't know what to do for this part:
$NewName = $_.Name = "hello2"