thebusiness11
thebusiness11

Reputation: 521

Powershell Remove Symbolic Link Windows

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?

Upvotes: 31

Views: 34147

Answers (4)

js2010
js2010

Reputation: 27443

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

Upvotes: 0

Lance U. Matthews
Lance U. Matthews

Reputation: 16606

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

Upvotes: 20

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

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"

Upvotes: 50

Luisrorta
Luisrorta

Reputation: 159

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"

Upvotes: 6

Related Questions