Remove-Item doesn't work, Delete does
Anyone have any idea why Remove-Item
would work if it Delete
does?
Below script, I am getting a list of files that I would like to delete.
Using Remove-Item
, I am getting the following error:
VERBOSE: Performing a "Delete file" operation on the target "\\ UncPath \ Folder \ test.rtf". Delete item: Unable to delete item \\ UncPath \ Folder \ test.rtf: path access denied.
but when used Delete
, these files are deleted 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() }
source to share
I can finally reproduce this and IMO it seems like a bug. The reproduction must have an open source such as C $, but to set Deny Modify perms for a user in a file. When I do this, 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 notice that removing the parameter -Force
also deletes the file without error. The deny perms feature still allows me to delete a file from Windows Explorer, which makes me think the file should be deleted. So what's using the parameter -Force
? When I delve into ErrorRecord I see the following:
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)
The parameter seems to be -Force
trying to set (more likely to reset) the attributes and the file permissions do not 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 try first as if -Force
not present, and if that fails, try resetting the attributes.
source to share
powershell can act strange with UNC path, I think it adds UNC path to current provider you can check 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 full path, we are telling powershell to use the file system provider , which solves the problem. you can also specify a vendor likeremove-item filesystem::\\uncpath\folder
source to share