14

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)

Mastacheata
  • 1,866
  • 2
  • 21
  • 32

5 Answers5

14

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")
Matt
  • 45,022
  • 8
  • 78
  • 119
  • 1
    Thx for the link. Apparently it's still somewhat broken in PS5.1. `New-Item -Type symboliclink -Path D:\test -Value D:\testtest` `Remove-Item "D:\test"` Fails due to missing Force parameter (after asking for confirmation): `Remove-Item "D:\test" -Force` and `Remove-Item "D:\test" -Recurse` Fails due to conflict between marker in request and analysis point (freely translated from german) after asking for confirmation. `Remove-Item "D:\test" -Force -Recurse` Fails the same but doesn't need confirmation. – Mastacheata May 19 '17 at 20:34
  • With a junction however you can remove it without having to confirm by adding both force and recurse: i.e.: `New-Item -Type junction -Path D:\test -Value D:\testtest` `Remove-Item "D:\test" -Force -Recurse"` – Mastacheata May 19 '17 at 20:37
  • While doing this command, I'm getting an error. (Run the PowerShell command in admin mode) ----------- Confirm The item at C:\Users\*******\OneDrive\Desktop\FolderToDelete has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y Remove-Item : Access to the cloud file is denied At line:1 char:1 + Remove-Item -path "C:\Users\*******\OneDrive\D ... – Jyothis T S Jan 24 '22 at 05:35
  • @JyothisTS I think your problem is slightly different in that it's not really a junction, but instead some special file attribute which makes the cloud-only files that have not been persisted to your drive impossible to delete via Powershell. Best to ask the question yourself with some more context to it. – Mastacheata Mar 03 '22 at 17:23
2

have a try on this "command-let":

cmd /c rmdir .\Target

source:Powershell Remove-Item and symbolic links

ncowboy
  • 1,311
  • 2
  • 13
  • 19
  • While that is technically correct, it's not really what I was looking for. I've clarified my question to make it more clear I was looking for a PowerShell native Cmdlet and not a let-cmd-do-it hack. – Mastacheata May 19 '17 at 20:41
2

Simple command -

rm [path of file] -Force
Sidharth Taneja
  • 548
  • 6
  • 7
2

I know this post is old, but for anyone looking into this in 2023, you can use the following PS cmdlet-combination to remove a junction but not its contents :

$junction = Get-Item -Path <path_to_junction>
$junction.Delete()

That path is the actual junction path, not the parent path.

This can be condensed to :

(gi <path>).Delete()
Silloky
  • 147
  • 13
0

After search by Google for a long time, I found the answer:

function Remove-Any-File-Force ($Target) {
    if ( Test-Path -Path "$Target" ){
        & $env:SystemRoot\System32\ATTRIB.exe -S -H -R "$Target" >$null 2>$null
    } else {
        return
    }
    $TargetAttributes = (Get-Item -Path $Target -Force).Attributes.ToString()
    if ($TargetAttributes -match "ReparsePoint") {
        if ($TargetAttributes -match "Archive") {
            Remove-Item -Path "$Target" -Force
        } else {
            try {
                & $env:SystemRoot\System32\cmd.exe /c rmdir /Q "$Target" >$null 2>$null
            } catch {
                try {
                    [io.directory]::Delete("$Target")
                } catch {
                    Remove-Item -Path "$Target" -Force
                }
            }
        }    
    } else {
        if ($TargetAttributes -match "Directory") {
            Remove-Item -Path "$Target" -Force -Recurse
        } else {
            Remove-Item -Path "$Target" -Force
        }
    }
}
Ralph Lee
  • 11
  • 2