PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

`Remove-Item -WhatIf` on Junctions and SymbolicLinks (ReparsePoints) doesn't honor `-WhatIf`, removes the item anyway.

Open msftrncs opened this issue 2 years ago • 4 comments

Prerequisites

Steps to reproduce

While testing deleting symbolic links with Remove-Item in Windows PowerShell I ran in to issues and switched to PowerShell 7.3.4, where the command ran as expected, until I realized it deleted the files even though I had used the -WhatIf parameter.

Specifically I was deleting a symbolic link, but I also tested it on a junction point.

The actual command:

Remove-Item "$_\Documents" -force -whatif

$_ contained the directory info object of a user's folder on a file store. I was using the local file store at the time, so a local hard drive, not UNC paths.

I believe Windows PowerShell has the same issue, as it was providing an error regarding deleting the symbolic link after printing the verbose 'whatif:' response, due to some mismatch of tag information.

Expected behavior

Expected the file/folder to not be deleted.

Actual behavior

Folder was deleted even though the verbose output indicated 'whatif:'

What if: Performing the operation "Remove Directory" on target "E:\Shares\Users Folders\RNE_LdrFF\Documents".

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

msftrncs avatar May 26 '23 01:05 msftrncs

This does appear to be specific to links and force appears to be a red herring. (It is LESS forceful in Remove-Item than some other things)

image

First line creates a link, Second shows it is there Third runs del with -whatif (no -force) What if message appears Fourth shows the item has gone.

It meets my definition of a bug. I'm not sure if it is the cmdlet or the provider at fault, so I'm not going to change the tags for now.

@msftrncs Can you check that the simpler repro I've shown is valid, and possibly update the title ?

jhoneill avatar May 26 '23 15:05 jhoneill

@jhoneill, you appear to be correct, the -force is not required in PowerShell 7 at all, the symbolic link is deleted even though -WhatIf was specified, and it only appears to happen for ReparsePoint items.

image

Will update title accordingly.

msftrncs avatar May 30 '23 14:05 msftrncs

@msftrncs Thanks for that. I'll try to get some attention for this.

jhoneill avatar May 30 '23 15:05 jhoneill

It didn't make it onto the agenda for this week's cmdlet working group meeting but I'll try to get it onto a future one

I have marked this as a bug. It's unclear if the bug is in the the Remove-Item cmdlet or the File System provider - my hunch is the cmdlet because that's where whatif should be handled, but it might be a result of something the provider does.

jhoneill avatar Jun 08 '23 16:06 jhoneill

This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.

ghost avatar Jul 08 '23 05:07 ghost