删除powershell中的子目录,包括符号链接

Jul*_*ndy 6 windows powershell scripting

目前遇到问题,我目前正在这样做:

Get-ChildItem $PATH -Recurse -ErrorAction SilentlyContinue | 
  Where-Object {($_.Attributes -notmatch '\"Directory\"') -and  
              ($_.LastWriteTime -lt (Get-Date).AddHours(-12))}| 
    Remove-Item -Force -Recurse
Run Code Online (Sandbox Code Playgroud)

现在,如果我没有符号链接,它会很好地删除,但我有。我收到此错误:

Remove-Item :请求中指定的标记与重新分析点中存在的标记不匹配第 line:1 char:184 + ... ($_.LastWriteTime -lt (Get-Date).AddHours(- 12))}| 删除项目 -Force + ~~~~~~~~~~~~~~~~~~ + CategoryInfo :未指定:(:) [删除项目],Win32Exception + ExcellentQualifiedErrorId :System.ComponentModel.Win32Exception,Microsoft。 PowerShell.Commands.RemoveItemCommand

我无法将 powershell 升级到 v6。它似乎与: https: //github.com/powershell/powershell/issues/621#issuecomment-289230180

有人有解决方法吗?

Mar*_* Ba 6

这个问题似乎在 PS 6 中得到了解决(参考:https://github.com/powershell/powershell/issues/621

在 PS 5.1 中,可以使用以下方法解决此问题:

$(get-item $theSymlinkDir).Delete()
Run Code Online (Sandbox Code Playgroud)

或者正如 LotPings 在针对此特定 Foreach-Object 循环的 Q 的注释中指出的那样:

|? LinkType -eq 'SymbolicLink'| % { $_.Delete() }
Run Code Online (Sandbox Code Playgroud)


Fra*_*ani 6

今天进行了测试PowerShell 7,不幸的是问题仍然存在:PowerShell 无法删除符号链接。

我必须通过提升的提示符键入来删除该目录cmd /c rmdir /s /q C:\Users\Your_User_Name\Your_Folder_Name