title: Max Folders to create with Powershell
body: <p>Using Powershell - trying to figure out how to divide up files into folders but limit the number of folders leaving the remaining files in the original location after reaching the max folders and files per folder.
  Below script works fine until I try to add the while $foldermax.</p>
  <pre><code>   $filesperfolder = 2
     $folderMax = 3
     $files = '*.txt'
     $sourcePath = &quot;Path to source files&quot;
     $destPath = &quot;Destination Path&quot;
     $i = 0;
     $folderNum = 1;
  
     Get-ChildItem &quot;$sourcePath\$files&quot; | % {
  
      while($folderNum -le $folderMax)
      {     
     New-Item -Path ($destPath + &quot;\&quot; + $folderNum) -Type Directory -Force}
     Move-Item $_ ($destPath + &quot;\&quot; + $folderNum);
  
      $i++;
  
     if ($i -eq $filesperfolder){
          $folderNum++;
          $i = 0 ;
      }
    }
  </code></pre>