PowerShell-如何从文件夹中删除所有内容,而不是从子文件夹中删除所有内容的文件夹

问题描述 投票:-1回答:1

您能帮我...

我有一个具有这种结构的文件夹

MyFloder
|----File1
|----File2
|----File...
|----SubFolder01
|----SubFolder02
|----SubFolder...
|----ImportantSubFolder
        |----VeryImportantSubSubFolder
               |----ImportantFile01
               |----ImportantFile02
               |----ImportantFile...
               |----ImportantFolder01...
               |----ImportantFolder...

并且我需要删除“ MyFolder”中的所有内容,但不能删除“ VeryImportantSubSubFolder”中的所有内容,包括此“ VeryImportantSubSubFolder”中的所有文件/文件夹。

因此删除后,我只需要

MyFloder
|----ImportantSubFolder
        |----VeryImportantSubSubFolder
               |----ImportantFile01
               |----ImportantFile02
               |----ImportantFile...
               |----ImportantFolder01...
               |----ImportantFolder...

非常感谢!

powershell
1个回答
0
投票

这应该可以解决问题:

$folder = 'C:\myfolder\'

Get-ChildItem -Path $folder | ForEach-Object {

    if( !$_.PSIsContainer ) {
        Remove-Item -Path $_.FullName -Force | Out-Null
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.