Skip to content

On Windows, Remove-Item unexpectedly fails if the target directory path happens to end in a backslash #15248

@mklement0

Description

@mklement0

Steps to reproduce

# Switch to a temporary directory.
Push-Location -ea Stop ($tmpDir = (New-Item -Type Directory -Force (Join-Path Temp:/ $PID)).FullName)

try {

  # Create two directories with one file each in them.
  $null = New-Item -Type Directory -force foo1, foo2
  $null = New-Item -force foo1\t.txt, foo2\t.txt

  # Remove the first one with WITHOUT a trailing "\"
  { Remove-Item .\foo1 -Recurse -ErrorAction Stop } | Should -Not -Throw

  # Remove the second one with WITH a trailing "\"
  # This is also what you'd get with tab-completion.
  { Remove-Item .\foo2\ -Recurse -ErrorAction Stop } | Should -Not -Throw

} finally {
  # Clean up.
  Pop-Location; Remove-Item $tmpDir -Recurse
}

Expected behavior

Both tests should succeed: whether the target directory is specified with a trailing path separator or not should not make a difference.

Actual behavior

On Windows, removal fails when the target path ends in \:

Cannot remove item C:\Users\jdoe\AppData\Local\Temp\8656\foo2\: The directory is not empty.

Note:

  • The problem only occurs if the target dir. is non-empty.
  • It seems to have been introduced in v7.2.0-preview.5

Environment data

PowerShell Core v7.2.0-preview.5 on Microsoft Windows 10 Pro (64-bit; Version 20H2, OS Build: 19042.928)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.OS-WindowsWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core 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