title: PowerShell script to execute if threshold exceeded
body: <p>First off, sorry for the long post - I'm trying to be detailed!</p>
  
  <p>I'm looking to automate a work around for an issue I discovered.  I have a worker that periodically bombs once the "working" directory has more than 100,000 files in it.  Preventatively I can stop the process and rename the working directory to "HOLD" and create new working dir to keep it going.  Then I move files from the HOLD folder(s) back into the working dir a little bit at a time until its caught up.</p>
  
  <p>What I would like to do is automate the entire process via Task Scheduler with 2 PowerShell scripts.</p>
  
  <p><strong>----SCRIPT 1----</strong></p>
  
  <p>Here's the condition:</p>
  
  <ul>
  <li>If file count in working dir is greater than 60,000</li>
  </ul>
  
  <p>I find that<code>( [System.IO.Directory]::EnumerateFiles($Working)</code>is faster than <code>Get-ChildItem</code>.</p>
  
  <p>The actions:</p>
  
  <ul>
  <li><code>Stop-Service</code> for Service1, Service2, Service3</li>
  <li><code>Rename-Item -Path  "C:\Prod\Working\" -NewName "Hold"</code> or "Hold1","2","3",etc.. if the folder already exists --I'm not particular about the numeration as long as it is consistent so if it's easier to let the system name it HOLD, HOLD(1), HOLD(2), etc.. or append the date after HOLD then that's fine.</li>
  <li><code>New-Item C:\Prod\Working -type directory</code></li>
  <li><code>Start-Service</code> Service1, Service2, Service3</li>
  </ul>
  
  <p><strong>---SCRIPT 2----</strong></p>
  
  <p>Condition:</p>
  
  <ul>
  <li>If file count in working dir is less than 50,000</li>
  </ul>
  
  <p>Actions:</p>
  
  <ul>
  <li>Move 5,000 files from HOLD* folder(s) --Move 5k files from the HOLD folder until empty, then skip the empty folder and start moving files from HOLD1.  This process should be dynamic and repeat to the next folders.</li>
  </ul>
  
  <p>Before it comes up, I'm well aware it would be easier to simply move the files from the working folder to a Hold folder, but the size of the files can be very large and moving them always seems to take much longer.</p>
  
  <p>I greatly appreciate any input and I'm eager to see some solid answers!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's what I'm running for Script 2 -courtesy of Bacon</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>#Setup
  $restoreThreshold = 30000;  # Ensure there's enough room so that restoring $restoreBatchSize
  $restoreBatchSize = 500;   # files won't push $Working's file count above $restoreThreshold
  $Working = "E:\UnprocessedTEST\"
  $HoldBaseDirectory = "E:\"
  
  while (@(Get-ChildItem -File -Path $Working).Length -lt $restoreThreshold - $restoreBatchSize)
  {
      $holdDirectory = Get-ChildItem -Path $HoldBaseDirectory -Directory -Filter '*Hold*' | 
      Select-Object -Last 1;
                 
      if ($holdDirectory -eq $null)
      {
          # There are no Hold directories to process; don't keep looping
          break;
      }
  # Restore the first $restoreBatchSize files from $holdDirectory and store the count of files restored
      $restoredCount = Get-ChildItem $holdDirectory -File `
      | Select-Object -First $restoreBatchSize | Move-Item -Destination $Working -PassThru | 
       Measure-Object | Select-Object -ExpandProperty 'Count';
  
     # If less than $restoreBatchSize files were restored then $holdDirectory is now empty; delete it
      if ($restoredCount -lt $restoreBatchSize)
      {
          Remove-Item -Path $holdDirectory; 
                                             }
  }</code></pre>
  </div>
  </div>
  </p>