Fix `Remove-Item -WhatIf` removing directory symbolic links and junctions by Om-singhaI · Pull Request #28228 · PowerShell/PowerShell · GitHub
Skip to content

Fix Remove-Item -WhatIf removing directory symbolic links and junctions - #28228

Open
om singhal (Om-singhaI) wants to merge 1 commit into
PowerShell:masterfrom
Om-singhaI:fix/remove-item-whatif-symlink
Open

om singhal (Om-singhaI) wants to merge 1 commit into
PowerShell:masterfrom
Om-singhaI:fix/remove-item-whatif-symlink

Conversation

@Om-singhaI

Copy link
Copy Markdown

PR Summary

Remove-Item -WhatIf no longer deletes directory symbolic links and junctions, and answering No to -Confirm now keeps them as well.

Fix #16664

RemoveDirectoryInfoItem stores the ShouldProcess answer in continueRemoval, but the branch that detaches a symbolic link or junction ran before that value was checked, called directory.Delete() and returned. The fix only takes that branch when continueRemoval is true. File symbolic links go through RemoveFileInfoItem, which already checks ShouldProcess, so they were not affected.

I added two tests to the Remove-Item and hard/symbolic links context in FileSystem.Tests.ps1: one for a symbolic link to a directory on all platforms and one for a junction on Windows. Each runs Remove-Item -WhatIf and Remove-Item -Recurse -Force -WhatIf, then checks that the link and the target are still there.

PR Context

People use -WhatIf to preview a removal, so losing the link here is real data loss. The issue has been open since 2021, was reproduced again on 7.6.4, and #19714 was closed as a duplicate of it.

I tested on macOS arm64 with a local Debug build of master at fbb4fd5. Without the fix the new symbolic link test fails because the link is gone after -WhatIf. With the fix it passes, and the rest of FileSystem.Tests.ps1 and Remove-Item.Tests.ps1 pass on macOS with the CI and Feature tags, apart from one test that needs sudo, which I did not run. I also piped N into the -Confirm prompt and the link now stays. I could not run the junction test myself since I do not have a Windows machine.

PR Checklist

`RemoveDirectoryInfoItem` asks `ShouldProcess` for the directory, but
the branch that detaches symbolic links and junctions ran before the
answer was checked. So `-WhatIf`, or answering No to `-Confirm`, still
removed the link.

Only take that branch when `ShouldProcess` approved the removal, and
add tests for a directory symbolic link and a junction.

Fix PowerShell#16664
Copilot AI balanced review requested due to automatic review settings October 16, 2026 23:33
@Om-singhaI
om singhal (Om-singhaI) requested a review from a team as a code owner October 16, 2026 23:33
@azure-pipelines

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow guard addresses the deletion bug, with regression coverage for both affected link types and no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes #16664 by making directory symbolic-link and junction removal honor ShouldProcess.

Changes:

  • Prevents link deletion with -WhatIf or a declined -Confirm.
  • Adds regression tests for directory symbolic links and Windows junctions, including recursive, forced previews.
File Description
test/​powershell/​Modules/​Microsoft.PowerShell.Management/​FileSystem.Tests.ps1 Verifies preview operations preserve links and target files.
src/​System.Management.Automation/​namespaces/​FileSystemProvider.cs Guards link deletion with the ShouldProcess result.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove-Item removes the symbolic links and junctions when used with -WhatIf parameter

2 participants