windows - Remove-Item 不起作用,Delete 起作用

标签 windows powershell

有谁知道为什么 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/

相关文章:

powershell - 如何将 AAD 组设置为 AAD 应用程序的所有者?

windows - 命名空间扩展中用于删除操作的入口点是什么?

java - 下面的java代码是否保证对Windows中未打开的文件进行独占锁定?

c++ - Qt 将代码从 Linux 迁移到 Windows

git - oh-my-posh 提示中未呈现的字符

powershell - 有没有办法在Powershell中以编程方式检查文件上的数字签名?

python - 我的 Py_NoneStruct 符号(python、boost.python)在哪里?

windows - 在 Sublime Text 2 中使用 Ctrl+D 进行多项选择时如何跳过匹配项?

powershell - 取消提升 PowerShell session

regex - 替换 Powershell 字符串中的路径