Skip to content

Remove-Item -Recurse against folders should use a .NET method on Windows instead of recursive list-and-delete #15028

@Sidneys1

Description

@Sidneys1

Problem

Possibly a non-issue, but this became apparent when trying to excise a poorly-name directory on Windows ('..). The folder was non-empty, and could not be entered with PowerShell, CMD, or Explorer to empty.

$files = Get-ChildItem;
$dir = $files[0]; # This is the badly-named directory

Remove-Item -Recursive -Force $dir; # Fails
# I assume because it tries to walk the contents and remove them individually,
# but the oddly-named directory "'.." cannot have its contents enumerated

$dir.Delete($true); # Succeeds, is System.IO.DirectoryInfo.Delete(bool recurse)

Solution

It would possibly speed up and increase the resilience of Remove-Item -Recursive operations against filesystem providers if PowerShell shortcutted directory removals to call the .NET System.IO.DirectoryInfo.Delete(bool recurse) method instead of recursively listing-and-deleting contents.

Testing

For testing purposes, the folder '.. was generated in an Angular project with the typo'd command ng generate component "'..\foo".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-Providerspecific to the FileSystem providerNeeds-TriageThe issue is new and needs to be triaged by a work group.Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management moduleWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions