Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() }
like image 444
Lieven Keersmaekers Avatar asked Sep 01 '14 13:09

Lieven Keersmaekers


People also ask

How do you delete something that won't delete?

To do this, start by opening the Start menu (Windows key), typing run, and hitting Enter. In the dialogue that appears, type cmd and hit Enter again. With the command prompt open, enter del /f filename, where filename is the name of the file or files (you can specify multiple files using commas) you want to delete.

How do I force something to open and delete?

Press Shift + Delete to force delete a file or folder If the problem is due to the Recycle Bin, you can select the target file for folder, and press Shift + Delete keyboard shortcut to permanently delete it. This way will bypass the Recycle Bin.


2 Answers

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

like image 102
Loïc MICHEL Avatar answered Oct 15 '22 22:10

Loïc MICHEL


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.

like image 41
Keith Hill Avatar answered Oct 15 '22 22:10

Keith Hill