6

Currently experiencing issue, I'm currently doing this:

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

Now, it would delete fine IF I didn't have symlinks, but I do. I am getting this error:

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:184 + ... ($_.LastWriteTime -lt (Get-Date).AddHours(-12))}| Remove-Item -Force + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Remove-Item], Win32Exception + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.RemoveItemCommand

I'm unable to upgrade powershell to v6. It seems to be related to:https://github.com/powershell/powershell/issues/621#issuecomment-289230180

Anyone have a workaround?

Julian Alwandy
  • 321
  • 1
  • 4
  • 13
  • 3
    You could do a separate run with `|? LinkType -eq 'SymbolicLink'|%{$_.Delete()}` [Reference](https://stackoverflow.com/a/45537590/6811411) –  Jul 03 '18 at 19:03
  • Yeah, that was something I was looking for. Thanks! – Julian Alwandy Jul 05 '18 at 19:41

2 Answers2

6

This problem seems to be fixed in PS 6 (ref: https://github.com/powershell/powershell/issues/621)

In PS 5.1 one can work around it by using:

$(get-item $theSymlinkDir).Delete()

or as LotPings noted in the comments to the Q for this specific Foreach-Object loop:

|? LinkType -eq 'SymbolicLink'| % { $_.Delete() }
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • I would say in PS 6 they've chosen to ignore it. I have the same problem with ntfs junction points under user profiles in windows 10. But cmd works. – js2010 Aug 01 '19 at 17:12
  • @js2010 - you mean to say it is not fixed in PS6 ? – Martin Ba Aug 02 '19 at 10:03
  • I'm wrong. Deleting dir symbolic links work in ps 6. The problem I'm dealing with is more bizarre. Reparsepoints with no target. – js2010 Aug 02 '19 at 12:18
6

Tested today on PowerShell 7 and unfortunately the issue is still there: PowerShell cannot delete symlink.

I had to delete the directory through an elevated prompt typing cmd /c rmdir /s /q C:\Users\Your_User_Name\Your_Folder_Name

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113