Use PowerShell to Separate Files into Subfolders by File Count

By using this PowerShell script, you can take a folder that contains a lot of files and separate them into subfolders by a specified number.

The below script is set to divide by 10 files, but it can be changed to whatever number you want.

$sourceDir='Y:\Scan Jobs\Hacienda Completed Final'

$path='Y:\Scan Jobs\Hacienda Completed Final Separated'
#md $sourceDir
#md $path
#make dummy files
#1..180000|%{New-Item -Path "C:\Sources" -ItemType File -Name "$_.txt"}
$files=(GCI -Path "$sourceDir").fullname

for ($i=0;$i -lt $files.Count ;$i+=10){
Write-Host $i -BackgroundColor Green
Start-Sleep 1
$targetdir=Join-Path $path $i;

$files[$i..($i+9)] |%{
If(!(Test-Path $targetdir -PathType Container)){[void](new-item -Path $path -name "$i" -Type Directory)}
#echo $targetdir
#echo $_
 $_|Move-Item -Destination $targetdir -Force

}

}

NOTES:
- Make sure the directory paths are different

- The folder you are dividing cannot contain any subfolders or the entire folder will be treated as 1 file