How to recursively delete an entire directory with PowerShell 2.0?

前端 未结 18 2093
南旧
南旧 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:56

    Another useful trick:

    If you find a lot of files with same or similar name convention (like mac file with dot prefix name... that famous file pulltion), you can easily remove them with one single line from the powershell like this:

    ls -r .* | rm
    

    This line is going to remove all files with a dot in the begining of the name inside the current directory, and all files with same circumstances inside other folders inside this directory too. Be aware about it when using it. :D

    0 讨论(0)
  • 2020-12-07 07:58

    I used:

    rm -r folderToDelete
    

    This works for me like a charm (I stole it from Ubuntu).

    0 讨论(0)
  • 2020-12-07 07:59

    Really simple:

    remove-item -path <type in file or directory name>, press Enter
    
    0 讨论(0)
  • 2020-12-07 07:59

    To delete the complete contents including the folder structure use

    get-childitem $dest -recurse | foreach ($_) {remove-item $_.fullname -recurse}
    

    The -recurse added to remove-item ensures interactive prompts are disabled.

    0 讨论(0)
  • 2020-12-07 08:02
    rm -r ./folder -Force    
    

    ...worked for me

    0 讨论(0)
  • 2020-12-07 08:04

    If you're committed to powershell, you can use this, as explained in the accepted answer:

    rm -r -fo targetDir
    

    But I've found it to be faster to use Windows Command Prompt

    rmdir /s/q targetDir
    

    In addition to being faster, another advantage to using the command prompt option is that it starts deleting files immediately (powershell does some enumeration first), so if something breaks while it's running, you've at least made some progress in deleting files.

    0 讨论(0)
提交回复
热议问题