title: UnauthorizedAccessException using Copy-Item on remote fileserver
body: <p>I'm trying to copy about 10 folders each containing a ~3KB .txt file onto a remote fileshare with some seconds latency. I'm using Powershells <code>Copy-Item</code> like this:</p>
  
  <pre><code>try
  {
      Copy-Item -Path $source -Destination $destination -Recurse -ErrorAction Stop
  }
  catch
  {
      Write-Error $_.Exception.ToString()
  }
  </code></pre>
  
  <p>The user running the script has read, write and execute permissions on the fileserver share and on the local source. </p>
  
  <p>On first run, the destination folder is empty. Everything works fine. 
  On second run, the files and folders already exist. So before running the code above I first run a check using <code>Test-Path</code> and in case the folder exists a delete using <code>Remove-Item</code> like this: </p>
  
  <pre><code>try
  {
      if(Test-Path -Path $path -ErrorAction Stop)
      {
           Remove-Item -Recurse -Path $path -ErrorAction Stop
      }
  }
  catch
  {
      Write-Error $_.Exception.ToString()
  }
  </code></pre>
  
  <p>Nobody else edits those files. However, when running the script a dozent times, once in a while, for a reason I don't understand, i'm suddenly getting <strong>UnauthorizedAccessException</strong> errors for some of the folders while copying. The exact error is: </p>
  
  <blockquote>
    <p>System.UnauthorizedAccessException: access denied
    ---> System.ComponentModel.Win32Exception: access denied
    in Microsoft.PowerShell.Commands.FileSystemProvider.NativeDirectoryExists(String
    path) in
    System.Management.Automation.SessionStateInternal.IsItemContainer(CmdletProvider
    providerInstance, String path, CmdletProviderContext context</p>
  </blockquote>
  
  <p><strong>please note: I'm getting those errors AFTER the deletion of the old files on the remote fileserver has compleated successfully.</strong></p>