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

Viewed 2023

It seems that Remove-Item -Recurse -Force cannot deal with removing symlinks. How do I recursively delete everything from a given directory with symlinks made all over the place?

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
3 Answers

The actual CMD equivalent to Remove-Item -Recurse -Force is:

cmd /c rmdir /s /q foo

as it will delete symlinks also in subdirectories. This seems to be good enough for me.

There's always cmd. Wmi and powershell can't even delete profiles these days because of links.

cmd /c del bar
Related