Remove-Item doesn’t work, Delete does

Question:

Does anyone have any idea why Remove-Item would fail while Delete works?


In below script, I get a list of files I’d like to delete.
Using Remove-Item I get following error message:

VERBOSE: Performing the operation “Remove File” on target
“\\UncPath\Folder\test.rtf”. Remove-Item : Cannot remove item
\\UncPath\Folder\test.rtf: Access to the path is denied.

but using Delete is deleting those files as we speak.

Script

Answer:

I can finally repro this and IMO it appears to be a bug. The repro is to have an open share like C$ but to set Deny Modify perms for the user on the file. When I do that, I observe this:

I also observe that removing the -Force parameter deletes the file without error as well. The deny perms still allow me to delete the file from Windows Explorer so that leads me to believe that the file should delete. So what is up with using the -Force parameter? When I delve into the ErrorRecord I see this:

It seems that the -Force parameter is trying to set (more likely reset) attributes and the permissions on the file don’t allow it e.g.:

So it seems to me that PowerShell should first try as if the -Force weren’t present and if that fails, then try resetting attributes.

Source:

Remove-Item doesn’t work, Delete does by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply