Rename this String- Help

i have a bunch of files on a ftp server. there named with different naming patterns

heres a couple


2065787459 by @ 1_17_36 PM.wav


they all share a 10 digit phone # eg. 8328924511

I need to extract just the phone # and rename the file just the phone# that was extracted

im stuck because there are all different patterns excluding the 10 digits

any help would be greatly appreciated

im using this for the 10 digits = ‘\d{10}’

Hi H.

You could try something like this:

Get-ChildItem -Path $sourcedir |
    foreach { 
        if ($_ -match "(\d{10})" ) {
            Rename-Item -Path $_.FullName -newName "$($matches[0])$($_.Extension)"

It’s not pretty, but it works :slight_smile:

thx let me try :slight_smile:

any way of adding the date to the name too?

it worked thank you!!

its working on 95% but im a couple im getting this error

Rename-Item : Cannot create a file when that file already exists.
At line:7 char:13

  •         Rename-Item -Path $_.FullName -newName "$($matches[0])$($_.Extension ...
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : WriteError: (C:\Users\Admini…175665351_1.wav:String) [Rename-It
      em], IOException
    • FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Then you might want to add a small test to check if the file already exists.
If it does a new uniquie name needs to be hammered by adding something to the name.
It could be any random number.

now this

Rename-Item : Cannot rename because the target specified represents a path or
device name.

The thing is you need to handle several scenarios here. One is the first time you identify a file based on name. The next time the script is executed it will find the same files once more.
I would proberbly add a prefix to the files that have been renamed and filter theese out the next time the script ran.

Then you need to ensure all files are renamed using a unique name. For this you could use a simple counter in a loop to create a suffix and add it to the filename

The following snippet kinda works in my test environment. For all files the script touches it will add a prefix ‘renamed-’ and will also skip any files that have a prefix of ‘renamed’

function Get-UniqueFileName {

        [ValidateScript({Test-Path $_})]


        [String]$Suffix = 'renamed',


    $base = "$path/$suffix-$BaseName"
    $newPath = "$base$Extention"
    while (Test-Path $newPath) {
        $newPath = Join-Path -Path $path -ChildPath "$suffix-$BaseName-$counter$Extention"
        Write-Verbose $newPath

    write $newPath

$sourcedir = 'D:\test'

Get-ChildItem -Path $sourcedir |
    foreach {
        if ($_ -match "(\d{10})" ) {
            if ($_ -notmatch "^renamed" ) {
                write $_
                Rename-Item -Path $_.FullName -newName (Get-UniqueFileName -Path $sourcedir -BaseName $matches[0] -Extention $_.Extension -Verbose )

thank you Stein you have been a tremendous help! :slight_smile:

no problem H