How to recursively delete an entire directory with PowerShell 2.0?

前端 未结 18 2151
南旧
南旧 2020-12-07 07:26

What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

I have learned from severa

18条回答
  •  孤街浪徒
    2020-12-07 07:53

    I took another approach inspired by @john-rees above - especially when his approach started to fail for me at some point. Basically recurse the subtree and sort files by their path-length - delete from longest to the shortest

    Get-ChildItem $tfsLocalPath -Recurse |  #Find all children
        Select-Object FullName,@{Name='PathLength';Expression={($_.FullName.Length)}} |  #Calculate the length of their path
        Sort-Object PathLength -Descending | #sort by path length descending
        %{ Get-Item -LiteralPath $_.FullName } | 
        Remove-Item -Force
    

    Regarding the -LiteralPath magic, here's another gotchya that may be hitting you: https://superuser.com/q/212808

提交回复
热议问题