Help with a script - to move files in a folder with similar name

Hi there!

I try to automate some work process for me.

I have the situation that I have a lot of files which are named differently. Please see below an example (but files can be up to 100)


I would like to create a script which creates just 1 folder for each exampe and move all 3 files there.
I was able to create a script which uses the example1.pd to create a folder named example1 and that for all of the ongoing numbers.

But I am having problems to let the other files, named _COT, _en to be moved into that folder.
I first tried a script which can move files with the same filename into a folder with the same file names. The script would check for the first 50 characters of the file name. But then of course it wont copy the files with the post-fixes.

I then tried to include in the script a function to exclude “_COT and _en” and move it then. But it wont work.

Does someone has an Idea how a script like that can look like?
I have attached my current script

$sourcePath = "C:\source"
$destinationPath = "C:\destination"
$excludeWords = @("_COT", "_en")

Get-ChildItem -Path $sourcePath -Filter "filename*" | ForEach-Object {
    $folderName = $_.Name.Substring(0, 50)
    $folderPath = Join-Path $destinationPath $folderName
    New-Item -ItemType Directory -Path $folderPath -Force | Out-Null
    $newFileName = $_.Name
    foreach ($word in $excludeWords) {
        $newFileName = $newFileName -replace $word, ""

    Move-Item -Path $_.FullName -Destination (Join-Path $folderPath $newFileName)

Since the “suffixes” of your filenames are connected with an underline you can split there and use this as the common property. This should be enough actually:

$sourcePath = "C:\source"
$destinationPath = "C:\destination"
Get-ChildItem -Path $sourcePath -Filter "filename*" |
    Select-Object -Property *, @{
        Name       = 'CommonBaseNamePart'
        Expression = { ($_.BaseName -split '_')[0] }
    } |
        ForEach-Object {
            $folderPath = Join-Path -Path $destinationPath -ChildPath $_.CommonBaseNamePart
            If (-not (Test-Path -Path $folderPath)) {
                New-Item -ItemType Directory -Path $folderPath -Force | Out-Null
            Move-Item -Path $_.FullName -Destination $folderPath

Please test with test files and folders!! :wink:

Hey Olaf,
thank you for looking into this!

I tried the script but it seems notthing is happening.

I have the following situation

in c:\destination I have folders named:


in c:\source i have files named as:


So i tried using this script to move the files from the source folder into the destination folder.
and that all example1*.pdf should move into folder example1, so that in the destination folder all 3 would be present.

Is that doable with that script idea?

Of course you have to adjust it to your environment. I assumed example1.pdf was just an example. So you have to provide a valid -Filter criteria for your Get-ChildItem command:

Instead of "filename*" you should use "example*" or you remove the -Filter completely when there are no other files in that forlder. :man_shrugging:t3:

And BTW: Please format your code or sample data as code. Thanks in advance. :+1:t3:

Thank you Olaf!

I have found a workaround.
First i let the script search for all filenames in that folder and create for each file a folder, but exclude the files which end with “_COT” or “_en”. After that I let the script search the source folder for filenames, which include the folder names and move them into that specific folder :slight_smile:


Did you actually read my last reply? I tested the code and it works. :man_shrugging:t3:

I had some problems with my adapted script - for the filenames I use have lots of “_” in it. So while I was waiting for your response I started to try different approaches.

Since my filenames have 3-5 “_” in it, the script cut off at different places and created a lot if folders, which I could not use as desired :)! But I think it would work out fine if the filenames are simpler! Thanks for looking into it. I still try to find out how Powershell thinks and how I should approach different

Hmmm … and why don’t you share some example file names having the same structure your real files names have??? :man_shrugging:t3:

The help we can provide heavily depends on the information we get. :man_shrugging:t3: :man_shrugging:t3: :man_shrugging:t3:

And what’s the criteria to split? Or in other words what part would you like to use?

And with “different places” you mean all underlines, right? :smirk: :wink:

If that’s an option I’d try this approach. But there might a way to accomplish what you need but you don’t see yet. So you show what you have to deal with. Maybe one of us has an idea. :wink:

Lucky you that it does not. It’s a computer. It does not think. It computes. :point_up:t3: :love_you_gesture:t3: :slightly_smiling_face: