Powershell Remove Symbolic Link Windows
Asked Answered
P

4

29

I am having issues when removing SymbolicLinks which I have created with New-Item:

New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\"

I need to modify the link because it has the wrong -Target, which should be:

New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"

How to remove that link and assign a new one? Alternatively, how to update the target path of the existing link?

Pyrogallate answered 6/8, 2017 at 21:42 Comment(3)
This is not removing the link but rather replacing/updating it. Have you tried the -Force parameter?Nary
Correct, I do not know how to remove the link- The second item is just showing what I needed it to be.Pyrogallate
It's absolutely absurd that Remove-Item on a symbolic directory link attempts to recursively delete the original folder.Durstin
M
47

Calling Delete() on the corresponding DirectoryInfo object should do the trick:

(Get-Item C:\SPI).Delete()
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"
Monicamonie answered 6/8, 2017 at 23:26 Comment(3)
(Get-Item C:\SPI).Delete() still works as expected: Removing the SymbolicLink and leaving the target folder intact. (Tested PS6 win10)Salena
Thx! It also works in older versions like Power Shell 4.Attending
Just don't use get-childitem or dir instead and delete all the files inside the folder!Imprest
N
20

If you want to change the target path of the existing symbolic link C:\SPI from "C:\Users\Chino\Dropbox (Reserve Membership)\" to "C:\Users\Chino\Dropbox (Reserve Membership)\SPI\" you do not need to delete the link beforehand. Simply including the -Force parameter to overwrite the link works for me in PowerShell 5.1 on Windows 10 Pro v1603:

New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI" -Force
Nary answered 6/8, 2017 at 23:40 Comment(0)
S
5

No way to update the symbolic link as far as I know. Gotta use CMD to remove symbolic link and you could then re-create it using your powershell script. You would do it like this in powershell.

cmd /c "rmdir C:\SPI"
Signalman answered 6/8, 2017 at 23:5 Comment(0)
I
0

"del" or "remove-item" works in powershell 7.

Imprest answered 10/12, 2023 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.