Renaming files within a folder - with wildcard?

Hi, I’ve had a lot of success with a PowerShell code I found on Youtube to bulk rename files, add prefixes, suffixes, etc.

Dir | Rename-Item –NewName { $_.name -replace “OLD”,“NEW” }

However, I’m not sure how to use wildcards.

For example, change the “#####” numbers in the list below to eg. “00000”

AAAAAA - 12345
BBBBBB - 44444
CCCCC - 65656

Would anybody be able to assist?

First things first …

Please don’t use aliasses in scripts or here in the forum. It makes your code harder to read and to understand.

then … When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Now …

Since -replace works with regex you simply use a proper pattern to select only the digits in your name string:

Get-ChildItem | 
    Rename-Item -NewName { $_.name -replace '(\d)+','00000' }

This should do the trick.

Thanks, this works well. And thanks for the information regarding code formatting in this forum.