replace names of all directiories and files in PS

给你一囗甜甜゛ 提交于 2019-11-30 21:05:08

Don't use the Name switch, it outputs only the names of the objects, not their full path. Try this:

Get-ChildItem -Recurse | `
   Where-Object {$_.Name -match ' '} | `
     Rename-Item -NewName { $_.Name -replace ' ','_' }

The issue here is that if there is no space in the file name the name does not change. This is not supported by Rename-Item. You should use Move-Item instead:

Get-ChildItem -recurse -name | ForEach-Object { Move-Item $_ $_.replace(" ", "_") }

Additionally, in your answer you missed the underscore in $_.replace(...) plus you where replacing spaces with an empty string. Included this in my answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!