Title: Remove-Item -Recurse with an -Include or -Filter argument mistakenly tries to remove the directory itself · Issue #16153 · PowerShell/PowerShell · GitHub
Open Graph Title: Remove-Item -Recurse with an -Include or -Filter argument mistakenly tries to remove the directory itself · Issue #16153 · PowerShell/PowerShell
X Title: Remove-Item -Recurse with an -Include or -Filter argument mistakenly tries to remove the directory itself · Issue #16153 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/16153
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Remove-Item -Recurse with an -Include or -Filter argument mistakenly tries to remove the directory itself","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nNote: \r\n* This issue has _partly_ been reported before, in the context of #3727, but it's worth creating a focused report.\r\n* The problem affects `-Exclude` too, but use of `-Exclude` with _file_ filters is treacherous and not meaningfully supported.\r\n\r\nWhen you combine `Remove-Item -Recurse` with either `-Include` or `-Filter`, only those items _inside_ the target directory subtree that match the pattern(s) are removed, as intended.\r\n\r\nHowever, if you invoke such a command with `.` as the input path, i.e. from _inside_ the target directory, the command fails, as an attempt is seemingly made to delete the directory _itself_:\r\n\r\n```powershell\r\n# Switch to a temporary directory.\r\nPush-Location -ea Stop ($tmpDir = (New-Item -Type Directory -Force (Join-Path Temp:/ $PID)).FullName)\r\n\r\ntry {\r\n\r\n$null = New-Item -Force 1.txt, sub/2.txt\r\n\r\nRemove-Item . -Recurse -Filter *.txt\r\n\r\n$null = New-Item -Force 1.txt, sub/2.txt\r\n\r\nRemove-Item . -Recurse -Include *.txt\r\n\r\n} finally {\r\n # Clean up.\r\n Pop-Location; Remove-Item $tmpDir -Recurse\r\n}\r\n```\r\n\r\n### Expected behavior\r\n\r\nBoth `Remove-Item` calls should succeed and delete all `*.txt` files from the current dir's subtree.\r\n\r\n\r\n### Actual behavior\r\n\r\nBoth commands fail.\r\n\r\n\r\n### Error details\r\n\r\n```none\r\nCannot remove the item at '\u003cfull-path-of-current-dir>' because it is in use.\r\n```\r\n\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPowerShell Core 7.2.0-preview.9\r\n```\r\n\r\nThe problem also affects Windows PowerShell.\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2021-09-24T21:29:50Z","url":"https://github.com/PowerShell/PowerShell/issues/16153"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b101b0a0-4a85-4d9c-aeb7-a60f27b4f630 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D1D6:1EDE7C:30E863:43ADEC:6AFDA412 |
| html-safe-nonce | 06e28dd0ef2f88179663012f54c54b51a2988c9b9cd32b08d2fe6fe477398b13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMUQ2OjFFREU3QzozMEU4NjM6NDNBREVDOjZBRkRBNDEyIiwidmlzaXRvcl9pZCI6IjQyODAwNzUwMzk4NjIwMDY4MDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 75b09fc492e45cd8bd62ae826385b1a08bf1c1830c8e2f8a1e9c0581599b6ceb |
| hovercard-subject-tag | issue:1006838938 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/16153/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae59e030e89f23885c0ee30c9494a1290376cdb3e46e8fc10db81b5c4e263a9a/PowerShell/PowerShell/issues/16153 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae59e030e89f23885c0ee30c9494a1290376cdb3e46e8fc10db81b5c4e263a9a/PowerShell/PowerShell/issues/16153 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0ddf53eb35c87f1f8df13dbf75c3bdadbfc6e969 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width