Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

Remove-Item doesn't work, Delete does

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

$files = gci \\UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) }

# This doesn't work
$files | Remove-Item -force -verbose

# But this does
$files | % { $_.Delete() }

Upvotes: 8

Views: 45978

Answers (2)

Keith Hill
Keith Hill

Reputation: 201632

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:

PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
ri : Cannot remove item \\Keith-PC\C$\Users\Keith\foo.txt: Access to the path is denied.
At line:1 char:43
+ gci '\\Keith-PC\C$\Users\Keith\foo.txt' | ri -for
+                                           ~~~~~~~
    + CategoryInfo          : InvalidArgument: (\\Keith-PC\C$\Users\Keith\foo.txt:FileInfo) [Remove-Item], ArgumentExc
   eption
    + FullyQualifiedErrorId : RemoveFileSystemItemArgumentError,Microsoft.PowerShell.Commands.RemoveItemCommand

PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Delete()} # <== this works!

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:

Message        : Access to the path is denied.
ParamName      :
Data           : {}
InnerException :
TargetSite     : Void set_Attributes(System.IO.FileAttributes)
StackTrace     :    at System.IO.FileSystemInfo.set_Attributes(FileAttributes value)
                    at Microsoft.PowerShell.Commands.FileSystemProvider.RemoveFileSystemItem(FileSystemInfo
                 fileSystemInfo, Boolean force)

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.:

PS> gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
Exception setting "Attributes": "Access to the path is denied."
At line:1 char:45
+ gci '\\Keith-PC\C$\Users\Keith\foo.txt' | %{$_.Attributes = 'Normal'}
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

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.

Upvotes: 4

Lo&#239;c MICHEL
Lo&#239;c MICHEL

Reputation: 26130

powershell may act strange with UNC path, I think it prepends the UNC Path with the current provider you can verify this with :

cd c:
test-path \\127.0.0.1\c$

returns TRUE

cd HKCU:
test-path \\127.0.0.1\c$

returns FALSE

when specifying the fullpath we're telling powershell to use the filesystem provider, that solves the problem. you could also specify the provider like remove-item filesystem::\\uncpath\folder

Upvotes: 9

Related Questions