Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Remove Junction

As of Windows 10 PowerShell is finally capable of creating Junctions and links natively.

Howerver the Remove-Item function seems to be unaware of the junction and tries to remove the directory asking for confirmation and if it should recursively delete items within.

So, the question is: Is there a way to remove a junction using PowerShell native Cmdlets? (i.e. without calling cmd)

like image 442
Mastacheata Avatar asked May 19 '17 01:05

Mastacheata


People also ask

How do I remove a symbolic link in PowerShell?

'To get rid of a symbolic link, you can simply delete it like you would any other file or directory. Just be careful to delete the link itself rather than the file or directory it's linking to. '

What does remove item do in PowerShell?

The Remove-Item cmdlet deletes one or more items. Because it is supported by many providers, it can delete many different types of items, including files, folders, registry keys, variables, aliases, and functions.

Is an NTFS junction point?

Junction Points (also commonly referred to as NTFS Junction or Directory Junction) are a type of reparse point which contains link to a directory that acts as an alias of that directory. Junction point work similar to a symbolic link but only for directories.


3 Answers

have a try on this "command-let":

cmd /c rmdir .\Target

source:Powershell Remove-Item and symbolic links

like image 185
ncowboy Avatar answered Sep 28 '22 04:09

ncowboy


Is there a way to remove a junction using PowerShell?

Currently, at least in PowerShell v5, this is considered "fixed". What you can do is use the -Force switch, else you will get an error calling the path an NTFS junction. The reason that I at least use the quotes on fixed is that using the switch will still make the message about children in the directory show up. Selecting Y will still only delete the junction in my testing using PSv5.

Remove-Item "C:\temp\junction" -Force -Confirm:$False

If that doesn't work for you or you don't have v5 you can use the .Net method to delete a directory. This appears to work correctly as well.

[io.directory]::Delete("C:\temp\junction")
like image 39
Matt Avatar answered Sep 28 '22 05:09

Matt


Simple command -

rm [path of file] -Force
like image 30
Sidharth Taneja Avatar answered Sep 28 '22 05:09

Sidharth Taneja