title: Move-Item causes "File Already Exists" error, despite folder having been deleted
body: <p>I have some code which deletes a folder, then copies files from a temporary directory to where that folder had been.</p>
  
  <pre><code>Remove-Item -Path '.\index.html' -Force
  Remove-Item -Path '.\generated' -Force -Recurse  #folder containing generated files
  
  #Start-Sleep -Seconds 10 #uncommenting this line fixes the issue
  
  #$tempDir contains index.html and a sub folder, "generated", which contains additional files.  
  #i.e. we're replacing the content we just deleted with new versions.
  Get-ChildItem -Path $tempDir | %{
      Move-Item -Path $_.FullName -Destination $RelativePath -Force 
  }
  </code></pre>
  
  <p>I get an intermittent error, <code>Move-Item : Cannot create a file when that file already exists.</code> on the Move-Item line for the <code>generated</code> path.</p>
  
  <p>I've been able to prevent this by adding a hacky <code>Start-Sleep -Seconds 10</code> after the second <code>Remove-Item</code> statement; though that's not a great solution.</p>
  
  <p>I assume the issue is that the Remove-Item statement completes / code moves on to the next line, before the OS has caught up with the actual file deletion; though that seems odd/worrying.  NB: There are ~2,500 files in the generated folder (all between 1-100 KBs).</p>
  
  <p>There are no other processes accessing the folders (i.e. I've even closed my explorer windows &amp; tested with this directory being excluded from my AV).</p>
  
  <p>I've considered other options:</p>
  
  <ul>
  <li><p>using <code>Copy-Item</code> instead of <code>Move-Item</code>.  I don't like this as it requires creating new files when they're not required (i.e. a copy is slower than a move)... It's faster than my current sleep hack; but still not ideal.</p></li>
  <li><p>deleting the files &amp; not the folder, then iterating through the subfolders &amp; copying files to the new locations.  This would work, but is a lot more code for something that should be simple; so I don't want to pursue that option.</p></li>
  <li><p>Robocopy would do the trick; but I'd prefer a pure PowerShell solution.  This is the option I'll eventually pick if there is no clean solution though. </p></li>
  </ul>
  
  <p><strong>Question</strong></p>
  
  <ul>
  <li>Has anyone seen this before?</li>
  <li>Is it a bug, or have I missed something?</li>
  <li>Is anyone aware of a fix / good workaround?</li>
  </ul>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Running the remove in a separate job (i.e. using the code below) did not resolve the issue.</p>
  
  <pre><code>Start-Job -ScriptBlock {
      Remove-Item -Path '.\index.html' -Force
      Remove-Item -Path '.\generated' -Force -Recurse  #folder containing generated files
  } | Wait-Job | Out-Null
  
  #$tempDir contains index.html and a sub folder, "generated", which contains additional files.  
  #i.e. we're replacing the content we just deleted with new versions.
  Get-ChildItem -Path $tempDir | %{
      Move-Item -Path $_.FullName -Destination $RelativePath -Force 
  }
  </code></pre>
  
  <p><strong>Update #2</strong></p>
  
  <p>Adding this works; i.e. rather than waiting a fixed time, we wait for the path to be removed / checking every second.  If it's not removed after 30 seconds we assume it's not going to be; so carry on regardless (which will cause the <code>move-item</code> to throw an error which gets handled elsewhere).</p>
  
  <pre><code># ... remove-item code ...
  Start-Job -ScriptBlock {
      param($Path)
      while(Test-Path $Path){start-sleep -Seconds 1}
  } -ArgumentList '.\generated' | Wait-Job -Timeout 30 | Out-Null
  # ... move-item code ...
  </code></pre>