Delete Symbolic Link in Windows

Viewed 8881

i want to delete some symbolic Links (Folder). I tried

(Get-Item "C:\Users\MIKROEG\AppData\Roaming\Microsoft\Windows\Network Shortcuts\*.*").Delete()

or

[string]$Nutzerpfad = "$env:APPDATA"
[string]$Destination = "$Nutzerpfad\Microsoft\Windows\Network Shortcuts\*.*"
[System.IO.Directory]::Delete($Destination, $true)

or

cmd /s rmdir C:\Users\MIKROEG\AppData\Roaming\Microsoft\Windows\Network Shortcuts\Test2

or

$alleordneranzeigen=Get-ChildItem -Path $Destination -Directory
foreach($ordner in $alleordneranzeigen)
{ Remove-Item -Path $ordner.FullName
}

but nothing works. The last one wants to delete recurse.

Can anyone help me?

[INK][1]

2 Answers

A directory symbolic link, typically created using mklink /D mysymlink C:\myfiles\somefile at the command line, can be removed like a directory using

rd <mysymlink>

(This is equivalent to the long form of the command, rmdir <mysymlink>. You did in fact try rmdir but this would have failed due to your unquoted path: cmd /s rmdir C:\Users\MIKROEG\AppData\Roaming\Microsoft\Windows\Network Shortcuts\Test2 .)

You may demonstrate this by making a backup copy of the directory in File Explorer, and using the rd command on your directory symlink. You will see that the symlink is removed, and the original directory and its contents are still present.

Warning about trying other ways: a directory symbolic link cannot be deleted like a file at the command line, using del. Doing so deletes the files in the directory!

In order to properly delete symbolic links using

mklink /d

you can just delete the symbolic link inside of windows explorer. This way you do not even need to bother using powershell.

HOWEVER:

If you are looking to delete the link with powershell DO NOT use

rmdir

Instead target the designated folder like this:

cmd /c rmdir .\Target

Related