I am new to PowerShell. When trying to write a simple script that deletes the contents of a folder and then fills it with files copied from a different folder, I always get
Have you try :
remove-item D:\path_A\* -recurse -force
After this error (and assuming at that point in it the most recent error):
$error[0] | fl * -force
will expand the details of the error and exception. That should give you more information.
Another thing to do is to switch on verbose logging
$VerbosePreference = "Continue"
to get more details about specifically what operation is being performed when the error occurs.
Finally PowerShell ISE includes a debugger which allows you to step through your script.
Is UAC enabled? If so try running your PowerShell session as 'Administrator'. It really looks like you don't have permission to delete the objects.
We have very restrictive security policies where I work and users not familiar with UAC get burned all the time.
In addition to reasons mentioned in the above posts, i've observed that "Access Denied" error is thrown when the file is being accessed by a separate process (In my case, i had to stop the server before Rename-Item could be run successfully).