Maximum number of folders to create

Trying to figure out how to set a maximum number of sub folders to create in a folder and then moving a specific number of files into each folder, leaving the remaining files in the original location after reaching the max folders and files per folder.
Below script works fine creating a “Sorted” subfolder and then subfolders beneath the subfolders and moving the files into it but I am having difficulty with adding the maximum number of subfolders to create under the “Sorted” subfolder…

 $filesperfolder = 2
 $folderMax = 3
 $files = '*.txt'
 $sourcePath = "Path to source files"
 $destPath = "Destination Path"
 $i = 0;
 $folderNum = 1;

 Get-ChildItem "$sourcePath\$files" | % {

while($folderNum -le $folderMax)
{     
New-Item -Path ($destPath + "\" + $folderNum) -Type Directory -Force
}
Move-Item $_ ($destPath + "\" + $folderNum);
$i++;
if ($i -eq $filesperfolder){
    $folderNum++;
    $i = 0 ;
   }
 }

You have to put the actions of increasing the $folderNum and the Move-Item stuff inside the while loop. Otherwise your while loop runs forever because your condition never changes.

Additional tip for the future:
Something like this becomes obvious when you simply output your variables to the console inside loops or after they’ve been changed in the code.