Rename-Item wildcards

Hi I have had a look around and am unable to find a solution to my issue (which I am surprised by as it seems so simple, so apologies if it has been answered elsewhere, I couldn’t find it).

I am attempting to do do a bulk rename of all .pdf files in a directory. filenames for example:

11-1000-E1754A-SCH-5001 WIP LOAD SCHEDULE.pdf
11-1000-E1754A-SCH-5002 WIP INSTRUMENT SCHEDULE.pdf
11-1000-E1754A-SCH-5003 WIP CABLE SCHEDULE.pdf

I am using the following line in Powershell:

Get-ChildItem *.pdf | Rename-Item -NewName {$_.Name -replace 'WIP*','0A.pdf'}

What I am expecting to happen is all renamed to the following:

11-1000-E1754A-SCH-5001 0A.pdf
11-1000-E1754A-SCH-5002 0A.pdf
11-1000-E1754A-SCH-5003 0A.pdf

but the wildcard in the rename statement is not picking up the trailing string after the ‘WIP*’ part of the filename.

What I actually get is this:

11-1000-E1754A-SCH-5001 0A.pdf LOAD SCHEDULE.pdf
11-1000-E1754A-SCH-5002 0A.pdf INSTRUMENT SCHEDULE.pdf
11-1000-E1754A-SCH-5003 0A.pdf CABLE SCHEDULE.pdf

any ideas where I am going wrong? or a better way of achieving what I am after?

TIA.

you’re pretty close. You just need a period before your asterisk because the -replace operator leverages regex. In regex ‘*’ means: match the previous token between zero and unlimited times. In your original code that previous token is the letter ‘P’.
By adding a period, ‘.’, this means match any character.

Get-ChildItem *.pdf | Rename-Item -NewName {$_.Name -replace 'WIP.*','0A.pdf'}

this leaves me with:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----          6/4/2024   7:27 AM              0 11-1000-E1754A-SCH-5001 0A.pdf
-a----          6/4/2024   7:27 AM              0 11-1000-E1754A-SCH-5002 0A.pdf
-a----          6/4/2024   7:27 AM              0 11-1000-E1754A-SCH-5003 0A.pdf
1 Like

Thankyou so much! I swear I tried this but clearly I didn’t that does what I am after thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.