如何在 PowerShell 中替换文档中文件名中的特殊字符

How to replace a special character from a file name in a document in PowerShell

我正在尝试 运行 通过文件夹中的几个文档编写 PowerShell 脚本,并替换可能包含某些特殊字符(主要是 $、#)的任何文档。但是我想同时做这两个。

$files1 | ForEach-Object {Rename-Item $_ ($_.FullName -replace "#", "")}

目前我收到以下错误

Rename-Item : Cannot find drive. A drive with the name '@{FullName=F' does not exist.At line:2 char:3 + Rename-Item $_ ($_.FullName -replace "#", "")

files1 变量的输出是这样的

FullName : F:\baggy_10_27 可能会放弃 - dondeal - EPT 2337 jonney - CLOSE OUT #025-17.msg
父文件夹:F:\baggy\
全路径长度:108
扩展名:消息
姓名:17_10_27 可能会放弃 - dondeal - EPT 2337 jonney - 结束 #025-17.msg
长度 : 121344
最后写入时间:2017 年 10 月 27 日 1:43:02 下午
 
FullName : F:\baggy_10_27 drum skyline un-cement 的 crickey 豁免 - #068-17 rewax RX - resent to OOE.msg
父文件夹:F:\baggy\
全路径长度:106
扩展名:消息
名称:17_10_27 drum skyline un-cement 的 crickey 豁免 - #068-17 rewax RX - 对 OOE.msg 不满
长度 : 386560
最后写入时间:10/27/2017 4:16:23 下午

这应该可以解决问题:

Get-ChildItem -Path X:\path-you-want | Rename-Item {$_.FullName -replace '#' -replace '$'}

试试这个:

$files1 | ForEach-Object {Rename-Item $_.FullName ($_.FullName -replace "#", "")}

但是,如果您不移动文件,我建议:

$files1 | ForEach-Object {Rename-Item $_.FullName ($_.Name -replace "#", "")}