Powershell重命名文件正则表达式

时间:2020-04-29 08:46:51

标签: windows powershell scripting

我的文件夹中有很多.pdf文件,其命名格式为以U.pdf开头的whatname空间随机码,我想删除U之前的任何命名。

示例名称格式:

Alex U153569.pdf->应该重命名为U153569.pdf

这是我到目前为止所拥有的:

foreach ($test in $testpdf) {
    Get-ChildItem -Filter *.pdf | Rename-Item -NewName { $_.name -Replace ????????? }
}

正确的是什么?

2 个答案:

答案 0 :(得分:0)

这应该有效:

$_.name -Replace '.*\s(?=U)'

答案 1 :(得分:0)

无需测试,您应该可以使用split运算符(不需要正则表达式)。您可以在下面的空格上拆分,然后索引到第二个拆分[1]

$testpdf = Get-ChildItem *.pdf
foreach ($test in $testpdf) {
    Rename-Item $test.name -NewName (($test.name -split " ")[1])
}