Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何删除文件夹中的所有内容,但不删除子文件夹中包含所有内容的文件夹_Powershell - Fatal编程技术网

Powershell 如何删除文件夹中的所有内容,但不删除子文件夹中包含所有内容的文件夹

Powershell 如何删除文件夹中的所有内容,但不删除子文件夹中包含所有内容的文件夹,powershell,Powershell,我有一个这样结构的文件夹 MyFolder |----File1 |----File2 |----File... |----SubFolder01 |----SubFolder02 |----SubFolder... |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02

我有一个这样结构的文件夹

MyFolder |----File1 |----File2 |----File... |----SubFolder01 |----SubFolder02 |----SubFolder... |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02 |----ImportantFile... |----ImportantFolder01... |----ImportantFolder... 我的文件夹 |----文件1 |----文件2 |----文件。。。 |----子文件夹01 |----子文件夹02 |----子文件夹。。。 |----重要文件夹 |----非常重要的子文件夹 |----重要文件01 |----重要文件02 |----重要文件。。。 |----重要的文件夹01。。。 |----重要的文件夹。。。 我需要删除“MyFolder”中的所有内容,但不删除“VeryImportantSubSubFolder”,包括此“VeryImportantSubfolder”中的所有文件/文件夹

所以删除后我只需要

MyFolder |----ImportantSubFolder |----VeryImportantSubSubFolder |----ImportantFile01 |----ImportantFile02 |----ImportantFile... |----ImportantFolder01... |----ImportantFolder... 我的文件夹 |----重要文件夹 |----非常重要的子文件夹 |----重要文件01 |----重要文件02 |----重要文件。。。 |----重要的文件夹01。。。 |----重要的文件夹。。。
这应该可以做到:

$folder = 'C:\myfolder\'

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

    if( !$_.PSIsContainer ) {
        Remove-Item -Path $_.FullName -Force | Out-Null
    }
}