title: Remove-Item error: Cannot remove item [item path & name]: Access to the path '[item path & name]' is denied
body: <p>I'm new to PowerShell.</p>
  
  <p>I'm trying to automate the deployment of dll components from a folder on a source server to multiple folders on the destination server. This seems like it should be simple enough: copy components from source (deployment) folder on source server to folders on destination server, verify copies and finally delete components from deployment folder on source server.</p>
  
  <p>The copying of the files from the source server to the destination server is working without issue.
  However, when the script moves on to delete the components from the source server, I'm intermittently confronted with the error: 
  "Remove-Item error: Cannot remove item [item path &amp; name]: Access to the path '[item path &amp; name]' is denied."</p>
  
  <p>I've run this script several times; sometimes it completes with no problems, sometimes with the error. The error does not occur for every file to be deleted and seems to occur on different components each time it presents.</p>
  
  <p>Below is the function I have written to remove components and verify deletions:</p>
  
  <pre><code>function DeleteSourceFiles($srcPath) {
      # Announce delete
      OutputToHostAndLog ("Files will be removed from "+$srcPath+"...")
      OutputToHostAndLog "Removing files..."
  
      # Deletes all file items (i.e. all except folders) in source folder
      $filesToDelete=Get-ChildItem $srcPath | Where-Object {$_ -is [IO.FileInfo]}
      ForEach($item in $filesToDelete) {
          Remove-Item $srcPath\$item -force
  
          # Verify deletions       
          if(Test-Path($srcPath+"\"+$item)) {
              OutputToHostAndLog ("Delete failed: "+$item.Name)        
              $fail++
          }
          else {
              OutputToHostAndLog ($item.Name+" deleted successfully...")
          }
      }
   }
  </code></pre>
  
  <p>The use of the -force parameter with the Remove-Item cmdlet does not appear to have any effect on the issue. The files (again, different files with each failure) don't appear to be isReadOnly anyway.</p>
  
  <p>Similarly, running PowerShell as Administrator seems to have no effect, although Get-Acl for the source folder indicates that Administrator should have FullControl.</p>
  
  <p>Is this a permissions issue that I am missing?
  Any suggestions very much appreciated...</p>
  
  <p>EDIT:
  I updated my script thus:</p>
  
  <pre><code>function DeleteSourceFiles($srcPath) {
      # Announce delete
      OutputToHostAndLog ("Files will be removed from "+$srcPath+"...")
      OutputToHostAndLog "Removing files..."
      OutputToHostAndLog $gap
  
      # Delete all file items (i.e. all except folders) in source folder
      $filesToDelete=Get-ChildItem $srcPath | Where-Object {$_ -is [IO.FileInfo]} | ForEach {
          Remove-Item $_.FullName -Force
  
          # Verify deletions
          if(Test-Path($srcPath+"\"+$_)) {
              OutputToHostAndLog ("Delete failed: "+$_.Name)        
              $fail++
          }
          else {
              OutputToHostAndLog ($_.Name+" deleted successfully...")
          }
      }
  }
  </code></pre>
  
  <p>This seems to work OK although I'm still not sure why this arrangement should produce different results. In the interest of learning, any insights would be greatly appreciated...</p>