Powershell 通过第12个字符重命名第4个字符

Powershell 通过第12个字符重命名第4个字符,powershell,powershell-3.0,powershell-4.0,Powershell,Powershell 3.0,Powershell 4.0,这是我的 get-childitem "\\myfileserver\out\*" | foreach { rename-item $_ $_.Name.Replace("_123456_837P.", ".").Replace(".test.", ".sa.").Replace("_987654_837I." , ".") } 这是我要修复的文件名 99

这是我的

get-childitem "\\myfileserver\out\*" | foreach { rename-item $_ $_.Name.Replace("_123456_837P.", ".").Replace(".test.", ".sa.").Replace("_987654_837I." , ".") }
这是我要修复的文件名 999_987654_837I.74161.测试


我想从文件名中删除_987654_837I。我只是想给它重新命名,但这些数字可能会改变。因此,现在我想删除从u开始的第4个字符,并返回到“I”或第9个字符。

您可以使用正则表达式模式来获取所需的部分

请参见正则表达式示例+说明:

我使用正向查找强制正则表达式在行首(^)获取前3个字符,而不捕获它。将捕获以下12个字符,然后将其替换为“”


$regexReplacePattern=”(?同样值得一提的是,您不需要在foreach循环中使用重命名项。
Get ChildItem“\\myfileserver\out\*”| rename Item-NewName{$\.Name-replace'(?)?
$regexReplacePattern = '(?<=^.{3}).{12}'

'999_987654_837I.74161.test' -replace $regexReplacePattern, ''