TM Forums
Back to search

Help batch file rename in powershell (Win10)

#Post
1

I tried to remove the underscore from all files in a folder and replace with a space using the following command in powershell.
Dir | Rename-Item -NewName {$_.name -replace "_"," "}

The error I got was
Rename-Item : Source and destination path must be different.
At line:1 char:7
+ Dir | Rename-Item -NewName {$_.name -replace "_"," "}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\users\All\de...e_shortlist-
\ABC:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.Po-
werShell.Commands.RenameItemCo-
mmand
How do I change the destination folder then?

bit - 2020-12-31 08:00:00
2

I guess I'm being lazy. I have to scour the internet for the answer.

bit - 2020-12-31 08:01:00
3

have a look at the second and third examples here
https://ss64.com/ps/rename-item.html

king1 - 2020-12-31 08:43:00
4

This works in powershell for what you want to do.
get-childitem | foreach { rename-item $_ $_.Name.Replace("_",-
" ") }

Edited by nzstocked at 2:33 pm, Thu 31 Dec

nzstocked - 2020-12-31 14:32:00
5

Ok I got the job done with this.
get-childitem -Path *.* | rename-item -NewName {$_.name -replace "_"," "}

Thanks both.

bit - 2020-12-31 22:06:00
Free Web Hosting