有谁知道为什么 Remove-Item
会失败而 Delete
有效?
在下面的脚本中,我得到了我想要删除的文件列表。
使用 Remove-Item
我收到以下错误消息:
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.
但使用 Delete
会在我们说话时删除这些文件。
脚本
$files = gci \\UncPath\Folder| ?{ $_.LastWriteTime -le (Get-Date).addDays(-28) }
# This doesn't work
$files | Remove-Item -force -verbose
# But this does
$files | % { $_.Delete() }
最佳答案
powershell 可能对 UNC 路径表现得很奇怪,我认为它会在 UNC 路径前面加上当前提供者,您可以使用以下方法验证这一点:
cd c:
test-path \\127.0.0.1\c$
returns TRUE
cd HKCU:
test-path \\127.0.0.1\c$
returns FALSE
在指定完整路径时,我们告诉 powershell 使用文件系统提供程序,这样就解决了问题。您还可以指定提供程序,例如 remove-item filesystem::\\uncpath\folder
关于windows - Remove-Item 不起作用,Delete 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25606481/