Remove-Item -Recurse -Force 无法删除符号链接

Remove-Item -Recurse -Force can't remove symlinks

似乎 Remove-Item -Recurse -Force 无法处理删除符号链接。如何递归地删除给定目录中的所有内容,并在各处创建符号链接?

MWE:

PS C:\Users\Administrator\Desktop\test> mkdir foo
    Directory: C:\Users\Administrator\Desktop\test
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/30/2020  11:58 AM                foo

PS C:\Users\Administrator\Desktop\test> mkdir bar
    Directory: C:\Users\Administrator\Desktop\test
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        7/30/2020  11:58 AM                bar

PS C:\Users\Administrator\Desktop\test> New-Item -ItemType SymbolicLink -Path foo -Name bar -Value C:\Users\Administrator\Desktop\test\bar
    Directory: C:\Users\Administrator\Desktop\test\foo
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l        7/30/2020  11:59 AM                bar

PS C:\Users\Administrator\Desktop\test> Remove-Item -Recurse -Force foo
Remove-Item : There is a mismatch between the tag specified in the request and the tag present in the reparse point
At line:1 char:1
+ Remove-Item -Recurse -Force foo
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-Item], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand

看起来您 运行 遇到了与此处提到的相同的问题:

https://github.com/powershell/powershell/issues/621

此线程中提到的解决方法是:

Get-ChildItem $somepath -Attributes ReparsePoint | % { $_.Delete() }

或者您可以在这里收集更多信息:

https://github.com/PowerShell/PowerShell/pull/11331

总有cmd。由于链接,这些天 Wmi 和 powershell 甚至无法删除配置文件。

cmd /c del bar

相当于Remove-Item -Recurse -Force的实际CMD是:

cmd /c rmdir /s /q foo

因为它也会删除子目录中的符号链接。这对我来说似乎已经足够了。