Powershell rm -rec traverses symbolic links and removes items in target - by Jason R. Coombs

Status : 

 


23
0
Sign in
to vote
ID 727149 Comments
Status Active Workarounds
Type Bug Repros 12
Opened 2/29/2012 7:41:44 AM
Access Restriction Public

Description

It's apparently not possible to simply remove symbolic links to directories using Powershell. Instead, Powershell recursively removes the children. For example:

cmd /c mklink /d MyWindows C:\Windows

If you try to remove MyWindows using Powershell, it prompts to delete children, and if you confirm (or if -rec was supplied), it attempts to remove everything in the C:\Windows directory. I don't suppose I have to explain why this is dangerous and ugly.

cmd.exe behaves much better, and just removes the link, which is also congruent with the expected behavior on Unix.

I encounter this issue frequently when developing with an application that creates symbolic links to my Python installation (called a virtualenv). When I remove the virtualenv using Powershell, Powershell also removes libraries in the system Python installation.

I thought I had logged this bug before, but apparently I only asked about it from the community.
Sign in to post a comment.
Posted by rpseekell on 7/9/2012 at 5:12 AM
In PS3 RC on Windows 7, I make a symbolic link like you said, and then tried "Remove-Item .\myWindows -rec -WhatIf". I got an exception saying "myWindows in a NTFS junction point. Use the Force parameter to delete or modify." Then I added -Force, and it tried to delete all of the directories and files in C:\Windows. When I removed the -recurse parameter (so only -force remained), it asked me about -recurse missing, but basically did the same thing.
Clearly, remove-item is not meant for symbolic links, so my question is when will there be a cmdlet that removes symbolic links?