# Moving Files by Filename to different Directoires

**URL:** <https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005>\
**Category:** PowerShell Help\
**Created:** [April 3, 2022, 11:01am UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005 "2022-04-03T11:01:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [April 3, 2022, 11:01am UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/1 "2022-04-03T11:01:21Z")

</div>

Hi there, I try to clean a folder with thousands of files. The goal is to move the files to diffrent folders sortet by name. All files that start with the lettre A should move to a-folder - and so on.  
My script looks like that:

```auto
Get-ChildItem -Path 'D:\Test' |foreach-object {
if($_.name -like 'a*')
{move-item d:\test\a-folder}
elseif($_.name -like 'b*')
{move-item d:\test\b-folder}
elseif($_.name -like 'c*')
{move-item d:\test\c-folder}
}

```

The error I get:  
Cannot find path ‘D:\test\a-folder’ because it does not exist.  
Move-Item:

What it does is, it deletes all three folders: a-folder/b-folder/c-folder  
Any suggestions? Thanks so much in advance

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [April 3, 2022, 12:19pm UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/2 "2022-04-03T12:19:32Z")

</div>

Hi zwymar,

check the help for [Move-Item](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item?view=powershell-7.2) and [Get-ChildItem](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2).

Move-item is missing path and destination.

also you have the subfolders inside d:\test. if you do get-childitem the directory will be picked up as well. $\_.name will most likely cause error when you try to move.

try something like

```auto
#add -file to specify you only want the files and not the directories inside
Get-ChildItem -Path 'C:\Users\potato\Documents\test' -File |
ForEach-object {
    if($_.name -like 'a*') {
        # include path and desitination
        Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\a-folder
    }
    elseif($_.name -like 'b*') {
        Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\b-folder
    }
    elseif($_.name -like 'c*') {
        Move-Item -path $_.FullName -destination C:\Users\potato\Documents\test\c-folder
    }
}

```

test it out with a few files

Hopefully the pros will jump in to give more guidance if I’ve missed something.

---

<div class="post-metadata">

**Author:** ![zwymar](https://avatars.discourse-cdn.com/v4/letter/z/b5e925/32.png) [@zwymar](https://forums.powershell.org/u/zwymar)\
**Post date:** [April 3, 2022, 12:35pm UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/3 "2022-04-03T12:35:31Z")

</div>

Hi mawazo,  
perfect, it works fine.  
Enjoy your sunday!  
Cheers Martin

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [April 3, 2022, 9:33pm UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/4 "2022-04-03T21:33:27Z")

</div>

I think the main problem with the example in your question, and also the solution, by @mawazo, is the the repetitive code. I would do something like this:

```auto
$destinationRoot = 'E:\Temp\Test\'

Get-ChildItem E:\Temp\Test\ -File | 
    ForEach-Object {
        $inital = $_.BaseName.Substring(0,1)
        $destinationPath = Join-Path -Path $destinationRoot -ChildPath "$inital-folder"
        if (-not(Test-Path $destinationPath)) {
            New-Item -Path $destinationPath -ItemType Directory
        }
        Move-Item -Path $_ -Destination $destinationPath
    }

```

---

<div class="post-metadata">

**Author:** ![mawazo](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@mawazo](https://forums.powershell.org/u/mawazo)\
**Post date:** [April 3, 2022, 10:34pm UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/5 "2022-04-03T22:34:42Z")

</div>

Amazing! @matt-bloomfield, thanks for sharing 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/moving-files-by-filename-to-different-directoires/19005/6 "2024-05-16T20:18:07Z")

</div>


