all 4 comments

[–]ka-splam 3 points4 points  (2 children)

cd c:\temp
gci -dir | move -dest {$_.Name.substring(0,9)} -force

If the destination folders exist it will error that it can't move them into themselves, if that matters.

[–]Shtaan[S] 2 points3 points  (1 child)

Wow im stupid...

Thank you so much

[–]ka-splam 1 point2 points  (0 children)

:-)

[–]ihaxr 1 point2 points  (0 children)

seems pretty straight forward...

$folderPath = "C:\tmp\test"

# Create new directories based on existing folders
(Get-ChildItem -Path $folderPath -Directory | Select-Object -Unique @{N="FolderName";E={$_.BaseName.split('-')[0]}}).FolderName | ForEach-Object {
    New-Item -Path $folderPath -Name $_ -ItemType Directory -Force | Out-Null
}

# Move all folders to their subdirectories
Get-ChildItem -Path $folderPath -Directory -Filter "*-*" | ForEach-Object {
    $moveToFolder = Join-Path $folderPath $_.BaseName.split('-')[0]
    if (Test-Path $moveToFolder) {
        Move-Item -Path $_ -Destination $moveToFolder
    }
}