# Replace filenames

**URL:** <https://forums.powershell.org/t/replace-filenames/19566>\
**Category:** PowerShell Help\
**Created:** [June 11, 2022, 12:21pm UTC](https://forums.powershell.org/t/replace-filenames/19566 "2022-06-11T12:21:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 11, 2022, 12:21pm UTC](https://forums.powershell.org/t/replace-filenames/19566/1 "2022-06-11T12:21:27Z")

</div>

I download a file.  
Then I want to change the filename to filename without dots

- .S00E00.mkv,.avi or .mp4.  
The filename is everything before .S00  
Everything after E00 I do not want except extention.  
Send this file to an external HD with folder name = filename and subfolder with S00 name  
Is this possible?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 11, 2022, 12:36pm UTC](https://forums.powershell.org/t/replace-filenames/19566/2 "2022-06-11T12:36:39Z")

</div>

ingewll,  
Welcome to the forum. 👋🏽

> [@ingewll](#):
>
> Is this possible?

Without actually getting what the filename really looks like and what it should be in the end I’d say - Yes, it is possible.

> [@ingewll](#):
>
> I download a file.

But why would you like to do it for one file with PowerShell? And if you really want to do it in PowerShell - what have you tried so far? Are you aware that there are tools available for file rename jobs like PowerToys PowerRename?

> **[PowerToys PowerRename utility for Windows](https://learn.microsoft.com/en-us/windows/powertoys/powerrename)**
>
> A windows shell extension for bulk renaming of files

Please keep in mind this forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We expect you to make an own attempt to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 12, 2022, 5:02am UTC](https://forums.powershell.org/t/replace-filenames/19566/3 "2022-06-12T05:02:27Z")

</div>

Yes I have tried PowerRename and like it!  
This is how far I have gone  
$files = Get-ChildItem -Path /Users/curt-ingehenning/Downloads/\_KLARA  
foreach ($file in $files)  
{  
$newFileName=$file.Name.Replace(“.”," ")  
Rename-Item $file $newFileName  
}

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 12, 2022, 12:41pm UTC](https://forums.powershell.org/t/replace-filenames/19566/4 "2022-06-12T12:41:02Z")

</div>

OK, I assume it is about more than one file, isn’t it? 😉

Before we proceed: 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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

Regardless of that could you please post a few filenames as they are before and how they should look in the end?

Thank you.

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 12, 2022, 2:46pm UTC](https://forums.powershell.org/t/replace-filenames/19566/5 "2022-06-12T14:46:25Z")

</div>

Yes it is a few.

Examples:  
Americas.Funniest.Home Videos S32E09 720p HEVC x265-MeGusta.avi = Americas Funniest Home Videos.S32E09.avi  
Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv = Superman and Lois.S02E13.mkv  
Star.Trek.Strange.New.Worlds S01E06 1080p WEB H264-CAKES.mp4 = Star Trek Strange New Worlds.S01E06.mp4  
Alone.S09E02 720p WEB-DL AAC2 0 h264-LBR.mkv = Alone.S09E02.mkv

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 12, 2022, 2:54pm UTC](https://forums.powershell.org/t/replace-filenames/19566/6 "2022-06-12T14:54:26Z")

</div>

Again … please … 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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

And … I assume that are the filenames before, right? How should they look like after the renaming?

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 12, 2022, 2:56pm UTC](https://forums.powershell.org/t/replace-filenames/19566/7 "2022-06-12T14:56:03Z")

</div>

```auto
Americas.Funniest.Home Videos S32E09 720p HEVC x265-MeGusta.avi = Americas Funniest Home Videos.S32E09.avi
Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv = Superman and Lois.S02E13.mkv
Star.Trek.Strange.New.Worlds S01E06 1080p WEB H264-CAKES.mp4 = Star Trek Strange New Worlds.S01E06.mp4
Alone.S09E02 720p WEB-DL AAC2 0 h264-LBR.mkv = Alone.S09E02.mkv 			

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 12, 2022, 2:58pm UTC](https://forums.powershell.org/t/replace-filenames/19566/8 "2022-06-12T14:58:05Z")

</div>

First original and after = how I want it.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 12, 2022, 7:02pm UTC](https://forums.powershell.org/t/replace-filenames/19566/9 "2022-06-12T19:02:36Z")

</div>

For the samples you shared this should work:

```auto
$fileList = Get-ChildItem -Path /Users/curt-ingehenning/Downloads/_KLARA
foreach ($file in $fileList) {
    $newFileName = ($file.BaseName -replace '\.', ' ' -replace '\s*(S\d{1,2}E\d{1,2}).*', '$1') + $file.Extension
    Rename-Item -Path $file.FullName -NewName $newFileName
}

```

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 12, 2022, 7:45pm UTC](https://forums.powershell.org/t/replace-filenames/19566/10 "2022-06-12T19:45:21Z")

</div>

Maybe something like this ?

```auto
$files = get-childitem 'b:\PS_test\*'
    Foreach ($filename in $files){
    
    $SE = (($filename.name | select-string -pattern 'S(\d{1,2})E(\d{1,2})').Matches[0].Groups[0].value).ToString()
    
    $SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').trim()
    
    $NewFileName = $SeriesName +'.' + $SE + $filename.Extension
    
    $SeriesDir = $($filename.Directory.fullName + "\$SeriesName\" + "$($SE.split('E')[0])\")
    
        If(!(Test-Path $SeriesDir)){

        New-item -Path $SeriesDir -ItemType Directory

        }

        Move-Item -Path $filename -Destination "$SeriesDir$NewFileName" -force
        
}

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 13, 2022, 5:08am UTC](https://forums.powershell.org/t/replace-filenames/19566/11 "2022-06-13T05:08:57Z")

</div>

GOOD!  
It didn’t handle the last file right

```auto
Superman.and.Lois.S02E13.HDTV.x264-TORRENTGALAXY[TGx].mkv

```

A new directory and subdirectory was created but the file was untouched original! Maybe the brackets in the filename caused it. Can be strange letters in original filename like ( ) , .

Can I change the destination to an external HD like G:/\_TEST ?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 13, 2022, 6:30am UTC](https://forums.powershell.org/t/replace-filenames/19566/12 "2022-06-13T06:30:38Z")

</div>

So you used DejvDzi’s solution. 🤷🏽‍♂️ Great … so I can go back to sleep. 😴

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 13, 2022, 8:02am UTC](https://forums.powershell.org/t/replace-filenames/19566/13 "2022-06-13T08:02:24Z")

</div>

Yes I did. Thanks for your effort

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 13, 2022, 8:13am UTC](https://forums.powershell.org/t/replace-filenames/19566/14 "2022-06-13T08:13:01Z")

</div>

> [@ingewll](#):
>
> new directory and subdirectory was created but the fi

You can change destination to G:/\_TEST, if you need support-- write, I’ll be available tonight.

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 13, 2022, 8:58am UTC](https://forums.powershell.org/t/replace-filenames/19566/15 "2022-06-13T08:58:18Z")

</div>

I tried this but no

```auto
Move-Item -Path $filename -Destination 'G:\_TEST\*' "$SeriesDir$NewFileName" -force

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 13, 2022, 9:02am UTC](https://forums.powershell.org/t/replace-filenames/19566/16 "2022-06-13T09:02:42Z")

</div>

I tried this but no

```auto
$SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').replace('[',' ').replace(']',' ').trim()

```

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 13, 2022, 7:36pm UTC](https://forums.powershell.org/t/replace-filenames/19566/17 "2022-06-13T19:36:41Z")

</div>

Hi, try this:

```auto
$files = get-childitem 'G:\_test\*'
    Foreach ($filename in $files){
    
    $SE = (($filename.name | select-string -pattern 'S(\d{1,2})E(\d{1,2})').Matches[0].Groups[0].value).ToString()
    
    $SeriesName = (($filename.name -split("$SE"))[0]).replace('.',' ').trim()
    
    $NewFileName = $SeriesName +'.' + $SE + $filename.Extension
    
    $SeriesDir = $('G:\_test\' + "$SeriesName\" + "$($SE.split('E')[0])\")
   
        If(!(Test-Path $SeriesDir)){

        New-item -Path $SeriesDir -ItemType Directory

        }

   Move-Item -Path $([WildcardPattern]::Escape($filename.fullname)) -Destination "$SeriesDir$NewFileName" -force
        
}

```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [June 13, 2022, 9:51pm UTC](https://forums.powershell.org/t/replace-filenames/19566/18 "2022-06-13T21:51:22Z")

</div>

Easy fix. Change parameter ‘-Path’ to ‘-LiteralPath’ in:

> $files = get-childitem ‘b:\PS\_test\*’

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 14, 2022, 4:50am UTC](https://forums.powershell.org/t/replace-filenames/19566/19 "2022-06-14T04:50:17Z")

</div>

PERFECT!! 100%  
Thank you very much !!  
Inge

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 14, 2022, 5:53am UTC](https://forums.powershell.org/t/replace-filenames/19566/20 "2022-06-14T05:53:27Z")

</div>

Continues trying to compile this ps1 file to an exe.file and get this error message.  
Spelling is correct.  
Both Invoke-ps2exe and ReplaceTEST.ps1 is on desktop.

```auto
Invoke-ps2exe : The term 'Invoke-ps2exe' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ Invoke-ps2exe -inputFile 'C:\Users\ingew\Desktop\ReplaceTPB.ps1' -out ...
+ ~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Invoke-ps2exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundExceptiontype or paste code here

```

Don’t understand what is wrong or missing

[Next page](https://forums.powershell.org/t/replace-filenames/19566.md?page=2)
